| 1 |
/* |
| 2 |
* Copyright 2019 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 |
#include "src/gpu/ganesh/ops/AtlasPathRenderer.h" |
| 8 |
|
| 9 |
#include "include/core/SkMatrix.h" |
| 10 |
#include "include/core/SkPath.h" |
| 11 |
#include "include/core/SkRect.h" |
| 12 |
#include "include/core/SkSize.h" |
| 13 |
#include "include/gpu/GpuTypes.h" |
| 14 |
#include "include/gpu/ganesh/GrBackendSurface.h" |
| 15 |
#include "include/gpu/ganesh/GrContextOptions.h" |
| 16 |
#include "include/gpu/ganesh/GrDirectContext.h" |
| 17 |
#include "include/gpu/ganesh/GrRecordingContext.h" |
| 18 |
#include "include/gpu/ganesh/GrTypes.h" |
| 19 |
#include "include/private/base/SkAssert.h" |
| 20 |
#include "include/private/base/SkDebug.h" |
| 21 |
#include "include/private/base/SkSpan_impl.h" |
| 22 |
#include "include/private/gpu/ganesh/GrTypesPriv.h" |
| 23 |
#include "src/base/SkMathPriv.h" |
| 24 |
#include "src/base/SkVx.h" |
| 25 |
#include "src/core/SkIPoint16.h" |
| 26 |
#include "src/gpu/ganesh/GrCaps.h" |
| 27 |
#include "src/gpu/ganesh/GrClip.h" |
| 28 |
#include "src/gpu/ganesh/GrDirectContextPriv.h" |
| 29 |
#include "src/gpu/ganesh/GrDrawingManager.h" |
| 30 |
#include "src/gpu/ganesh/GrDynamicAtlas.h" |
| 31 |
#include "src/gpu/ganesh/GrPaint.h" |
| 32 |
#include "src/gpu/ganesh/GrRecordingContextPriv.h" |
| 33 |
#include "src/gpu/ganesh/GrRenderTargetProxy.h" |
| 34 |
#include "src/gpu/ganesh/GrRenderTask.h" |
| 35 |
#include "src/gpu/ganesh/GrStyle.h" |
| 36 |
#include "src/gpu/ganesh/GrSurfaceProxy.h" |
| 37 |
#include "src/gpu/ganesh/GrSurfaceProxyView.h" |
| 38 |
#include "src/gpu/ganesh/GrTexture.h" |
| 39 |
#include "src/gpu/ganesh/GrTextureProxy.h" |
| 40 |
#include "src/gpu/ganesh/SurfaceDrawContext.h" |
| 41 |
#include "src/gpu/ganesh/effects/GrModulateAtlasCoverageEffect.h" |
| 42 |
#include "src/gpu/ganesh/geometry/GrStyledShape.h" |
| 43 |
#include "src/gpu/ganesh/ops/AtlasRenderTask.h" |
| 44 |
#include "src/gpu/ganesh/ops/DrawAtlasPathOp.h" |
| 45 |
#include "src/gpu/ganesh/ops/GrOp.h" |
| 46 |
#include "src/gpu/ganesh/ops/TessellationPathRenderer.h" |
| 47 |
|
| 48 |
#include <algorithm> |
| 49 |
#include <utility> |
| 50 |
|
| 51 |
using namespace skia_private; |
| 52 |
|
| 53 |
namespace { |
| 54 |
|
| 55 |
// Returns the rect [topLeftFloor, botRightCeil], which is the rect [r] rounded out to integer |
| 56 |
// boundaries. |
| 57 |
std::pair<skvx::float2, skvx::float2> round_out(const SkRect& r) { |
| 58 |
return {floor(skvx::float2::Load(&r.fLeft)), |
| 59 |
ceil(skvx::float2::Load(&r.fRight))}; |
| 60 |
} |
| 61 |
|
| 62 |
// Returns whether the given proxyOwner uses the atlasProxy. |
| 63 |
template<typename T> bool refs_atlas(const T* proxyOwner, const GrSurfaceProxy* atlasProxy) { |
| 64 |
bool refsAtlas = false; |
| 65 |
auto checkForAtlasRef = [atlasProxy, &refsAtlas](GrSurfaceProxy* proxy, skgpu::Mipmapped) { |
| 66 |
if (proxy == atlasProxy) { |
| 67 |
refsAtlas = true; |
| 68 |
} |
| 69 |
}; |
| 70 |
if (proxyOwner) { |
| 71 |
proxyOwner->visitProxies(checkForAtlasRef); |
| 72 |
} |
| 73 |
return refsAtlas; |
| 74 |
} |
| 75 |
|
| 76 |
bool is_visible(const SkRect& pathDevBounds, const SkIRect& clipBounds) { |
| 77 |
auto pathTopLeft = skvx::float2::Load(&pathDevBounds.fLeft); |
| 78 |
auto pathBotRight = skvx::float2::Load(&pathDevBounds.fRight); |
| 79 |
// Empty paths are never visible. Phrase this as a NOT of positive logic so we also return false |
| 80 |
// in the case of NaN. |
| 81 |
if (!all(pathTopLeft < pathBotRight)) { |
| 82 |
return false; |
| 83 |
} |
| 84 |
auto clipTopLeft = skvx::cast<float>(skvx::int2::Load(&clipBounds.fLeft)); |
| 85 |
auto clipBotRight = skvx::cast<float>(skvx::int2::Load(&clipBounds.fRight)); |
| 86 |
static_assert(sizeof(clipBounds) == sizeof(clipTopLeft) + sizeof(clipBotRight)); |
| 87 |
return all(pathTopLeft < clipBotRight) && all(pathBotRight > clipTopLeft); |
| 88 |
} |
| 89 |
|
| 90 |
#ifdef SK_DEBUG |
| 91 |
// Ensures the atlas dependencies are set up such that each atlas will be totally out of service |
| 92 |
// before we render the next one in line. This means there will only ever be one atlas active at a |
| 93 |
// time and that they can all share the same texture. |
| 94 |
void validate_atlas_dependencies( |
| 95 |
const TArray<sk_sp<skgpu::ganesh::AtlasRenderTask>>& atlasTasks) { |
| 96 |
for (int i = atlasTasks.size() - 1; i >= 1; --i) { |
| 97 |
auto atlasTask = atlasTasks[i].get(); |
| 98 |
auto previousAtlasTask = atlasTasks[i - 1].get(); |
| 99 |
// Double check that atlasTask depends on every dependent of its previous atlas. If this |
| 100 |
// fires it might mean previousAtlasTask gained a new dependent after atlasTask came into |
| 101 |
// service (maybe by an op that hadn't yet been added to an opsTask when we registered the |
| 102 |
// new atlas with the drawingManager). |
| 103 |
for (GrRenderTask* previousAtlasUser : previousAtlasTask->dependents()) { |
| 104 |
SkASSERT(atlasTask->dependsOn(previousAtlasUser)); |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
#endif |
| 109 |
|
| 110 |
} // anonymous namespace |
| 111 |
|
| 112 |
namespace skgpu::ganesh { |
| 113 |
|
| 114 |
constexpr static auto kAtlasAlpha8Type = GrColorType::kAlpha_8; |
| 115 |
constexpr static int kAtlasInitialSize = 512; |
| 116 |
|
| 117 |
// The atlas is only used for small-area paths, which means at least one dimension of every path is |
| 118 |
// guaranteed to be quite small. So if we transpose tall paths, then every path will have a small |
| 119 |
// height, which lends very well to efficient pow2 atlas packing. |
| 120 |
constexpr static auto kAtlasAlgorithm = GrDynamicAtlas::RectanizerAlgorithm::kPow2; |
| 121 |
|
| 122 |
// Ensure every path in the atlas falls in or below the 256px high rectanizer band. |
| 123 |
constexpr static int kAtlasMaxPathHeight = 256; |
| 124 |
|
| 125 |
// If we have MSAA to fall back on, paths are already fast enough that we really only benefit from |
| 126 |
// atlasing when they are very small. |
| 127 |
constexpr static int kAtlasMaxPathHeightWithMSAAFallback = 128; |
| 128 |
|
| 129 |
// skbug.com/40043377 -- The way GrDynamicAtlas works, a single 2048x1 path is given an entire |
| 130 |
// 2048x2048 atlas with draw bounds of 2048x1025. Limit the max width to 1024 to avoid this landmine |
| 131 |
// until it's resolved. |
| 132 |
constexpr static int kAtlasMaxPathWidth = 1024; |
| 133 |
|
| 134 |
bool AtlasPathRenderer::IsSupported(GrRecordingContext* rContext) { |
| 135 |
#ifdef SK_BUILD_FOR_IOS |
| 136 |
// b/195095846: There is a bug with the atlas path renderer on OpenGL iOS. Disable until we can |
| 137 |
// investigate. |
| 138 |
if (rContext->backend() == GrBackendApi::kOpenGL) { |
| 139 |
return false; |
| 140 |
} |
| 141 |
#endif |
| 142 |
#ifdef SK_BUILD_FOR_WIN |
| 143 |
// skbug.com/40044606 There is a bug with the atlas path renderer on Direct3D, running on |
| 144 |
// Radeon hardware and possibly others. Disable until we can investigate. |
| 145 |
if (rContext->backend() == GrBackendApi::kDirect3D) { |
| 146 |
return false; |
| 147 |
} |
| 148 |
#endif |
| 149 |
const GrCaps& caps = *rContext->priv().caps(); |
| 150 |
auto atlasFormat = caps.getDefaultBackendFormat(kAtlasAlpha8Type, GrRenderable::kYes); |
| 151 |
return rContext->asDirectContext() && // The atlas doesn't support DDL yet. |
| 152 |
caps.internalMultisampleCount(atlasFormat) > 1 && |
| 153 |
// GrAtlasRenderTask currently requires tessellation. In the future it could use the |
| 154 |
// default path renderer when tessellation isn't available. |
| 155 |
TessellationPathRenderer::IsSupported(caps); |
| 156 |
} |
| 157 |
|
| 158 |
sk_sp<AtlasPathRenderer> AtlasPathRenderer::Make(GrRecordingContext* rContext) { |
| 159 |
return IsSupported(rContext) |
| 160 |
? sk_sp<AtlasPathRenderer>(new AtlasPathRenderer(rContext->asDirectContext())) |
| 161 |
: nullptr; |
| 162 |
} |
| 163 |
|
| 164 |
AtlasPathRenderer::AtlasPathRenderer(GrDirectContext* dContext) { |
| 165 |
SkASSERT(IsSupported(dContext)); |
| 166 |
const GrCaps& caps = *dContext->priv().caps(); |
| 167 |
#if defined(GPU_TEST_UTILS) |
| 168 |
fAtlasMaxSize = dContext->priv().options().fMaxTextureAtlasSize; |
| 169 |
#else |
| 170 |
fAtlasMaxSize = 2048; |
| 171 |
#endif |
| 172 |
fAtlasMaxSize = SkPrevPow2(std::min(fAtlasMaxSize, (float)caps.maxPreferredRenderTargetSize())); |
| 173 |
fAtlasMaxPathWidth = std::min((float)kAtlasMaxPathWidth, fAtlasMaxSize); |
| 174 |
fAtlasInitialSize = SkNextPow2(std::min(kAtlasInitialSize, (int)fAtlasMaxSize)); |
| 175 |
} |
| 176 |
|
| 177 |
bool AtlasPathRenderer::pathFitsInAtlas(const SkRect& pathDevBounds, |
| 178 |
GrAAType fallbackAAType) const { |
| 179 |
SkASSERT(fallbackAAType != GrAAType::kNone); // The atlas doesn't support non-AA. |
| 180 |
float atlasMaxPathHeight_p2 = (fallbackAAType == GrAAType::kMSAA) |
| 181 |
? kAtlasMaxPathHeightWithMSAAFallback * kAtlasMaxPathHeightWithMSAAFallback |
| 182 |
: kAtlasMaxPathHeight * kAtlasMaxPathHeight; |
| 183 |
auto [topLeftFloor, botRightCeil] = round_out(pathDevBounds); |
| 184 |
auto size = botRightCeil - topLeftFloor; |
| 185 |
return // Ensure the path's largest dimension fits in the atlas. |
| 186 |
all(size <= fAtlasMaxPathWidth) && |
| 187 |
// Since we will transpose tall skinny paths, limiting to atlasMaxPathHeight^2 pixels |
| 188 |
// guarantees heightInAtlas <= atlasMaxPathHeight, while also allowing paths that are |
| 189 |
// very wide and short. |
| 190 |
size[0] * size[1] <= atlasMaxPathHeight_p2; |
| 191 |
} |
| 192 |
|
| 193 |
void AtlasPathRenderer::AtlasPathKey::set(const SkMatrix& m, const SkPath& path) { |
| 194 |
fPathGenID = path.getGenerationID(); |
| 195 |
fAffineMatrix[0] = m.getScaleX(); |
| 196 |
fAffineMatrix[1] = m.getSkewX(); |
| 197 |
fAffineMatrix[2] = m.getTranslateX(); |
| 198 |
fAffineMatrix[3] = m.getSkewY(); |
| 199 |
fAffineMatrix[4] = m.getScaleY(); |
| 200 |
fAffineMatrix[5] = m.getTranslateY(); |
| 201 |
fFillRule = (uint32_t)GrFillRuleForSkPath(path); // Fill rule doesn't affect the path's genID. |
| 202 |
} |
| 203 |
|
| 204 |
bool AtlasPathRenderer::addPathToAtlas(GrRecordingContext* rContext, |
| 205 |
const SkMatrix& viewMatrix, |
| 206 |
const SkPath& path, |
| 207 |
const SkRect& pathDevBounds, |
| 208 |
SkIRect* devIBounds, |
| 209 |
SkIPoint16* locationInAtlas, |
| 210 |
bool* transposedInAtlas, |
| 211 |
const DrawRefsAtlasCallback& drawRefsAtlasCallback) { |
| 212 |
SkASSERT(!viewMatrix.hasPerspective()); // See onCanDrawPath(). |
| 213 |
|
| 214 |
pathDevBounds.roundOut(devIBounds); |
| 215 |
#ifdef SK_DEBUG |
| 216 |
// is_visible() should have guaranteed the path's bounds were representable as ints, since clip |
| 217 |
// bounds within the max render target size are nowhere near INT_MAX. |
| 218 |
auto [topLeftFloor, botRightCeil] = round_out(pathDevBounds); |
| 219 |
SkASSERT(all(skvx::cast<float>(skvx::int2::Load(&devIBounds->fLeft)) == topLeftFloor)); |
| 220 |
SkASSERT(all(skvx::cast<float>(skvx::int2::Load(&devIBounds->fRight)) == botRightCeil)); |
| 221 |
#endif |
| 222 |
|
| 223 |
int widthInAtlas = devIBounds->width(); |
| 224 |
int heightInAtlas = devIBounds->height(); |
| 225 |
// is_visible() should have guaranteed the path's bounds were non-empty. |
| 226 |
SkASSERT(widthInAtlas > 0 && heightInAtlas > 0); |
| 227 |
|
| 228 |
if (SkNextPow2(widthInAtlas) == SkNextPow2(heightInAtlas)) { |
| 229 |
// Both dimensions go to the same pow2 band in the atlas. Use the larger dimension as height |
| 230 |
// for more efficient packing. |
| 231 |
*transposedInAtlas = widthInAtlas > heightInAtlas; |
| 232 |
} else { |
| 233 |
// Both dimensions go to different pow2 bands in the atlas. Use the smaller pow2 band for |
| 234 |
// most efficient packing. |
| 235 |
*transposedInAtlas = heightInAtlas > widthInAtlas; |
| 236 |
} |
| 237 |
if (*transposedInAtlas) { |
| 238 |
std::swap(heightInAtlas, widthInAtlas); |
| 239 |
} |
| 240 |
// pathFitsInAtlas() should have guaranteed these constraints on the path size. |
| 241 |
SkASSERT(widthInAtlas <= (int)fAtlasMaxPathWidth); |
| 242 |
SkASSERT(heightInAtlas <= kAtlasMaxPathHeight); |
| 243 |
|
| 244 |
// Check if this path is already in the atlas. This is mainly for clip paths. |
| 245 |
AtlasPathKey atlasPathKey; |
| 246 |
if (!path.isVolatile()) { |
| 247 |
atlasPathKey.set(viewMatrix, path); |
| 248 |
if (const SkIPoint16* existingLocation = fAtlasPathCache.find(atlasPathKey)) { |
| 249 |
*locationInAtlas = *existingLocation; |
| 250 |
return true; |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
if (fAtlasRenderTasks.empty() || |
| 255 |
!fAtlasRenderTasks.back()->addPath(viewMatrix, path, devIBounds->topLeft(), widthInAtlas, |
| 256 |
heightInAtlas, *transposedInAtlas, locationInAtlas)) { |
| 257 |
// We either don't have an atlas yet or the current one is full. Try to replace it. |
| 258 |
auto currentAtlasTask = (!fAtlasRenderTasks.empty()) ? fAtlasRenderTasks.back().get() |
| 259 |
: nullptr; |
| 260 |
if (currentAtlasTask && |
| 261 |
drawRefsAtlasCallback && |
| 262 |
drawRefsAtlasCallback(currentAtlasTask->atlasProxy())) { |
| 263 |
// The draw already refs the current atlas. Give up. Otherwise the draw would ref two |
| 264 |
// different atlases and they couldn't share a texture. |
| 265 |
return false; |
| 266 |
} |
| 267 |
// Replace the atlas with a new one. |
| 268 |
auto dynamicAtlas = std::make_unique<GrDynamicAtlas>( |
| 269 |
kAtlasAlpha8Type, GrDynamicAtlas::InternalMultisample::kYes, |
| 270 |
SkISize{fAtlasInitialSize, fAtlasInitialSize}, fAtlasMaxSize, |
| 271 |
*rContext->priv().caps(), kAtlasAlgorithm); |
| 272 |
auto newAtlasTask = sk_make_sp<AtlasRenderTask>(rContext, |
| 273 |
sk_make_sp<GrArenas>(), |
| 274 |
std::move(dynamicAtlas)); |
| 275 |
rContext->priv().drawingManager()->addAtlasTask(newAtlasTask, currentAtlasTask); |
| 276 |
SkAssertResult(newAtlasTask->addPath(viewMatrix, path, devIBounds->topLeft(), widthInAtlas, |
| 277 |
heightInAtlas, *transposedInAtlas, locationInAtlas)); |
| 278 |
fAtlasRenderTasks.push_back(std::move(newAtlasTask)); |
| 279 |
fAtlasPathCache.reset(); |
| 280 |
} |
| 281 |
|
| 282 |
// Remember this path's location in the atlas, in case it gets drawn again. |
| 283 |
if (!path.isVolatile()) { |
| 284 |
fAtlasPathCache.set(atlasPathKey, *locationInAtlas); |
| 285 |
} |
| 286 |
return true; |
| 287 |
} |
| 288 |
|
| 289 |
PathRenderer::CanDrawPath AtlasPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const { |
| 290 |
#ifdef SK_DEBUG |
| 291 |
if (!fAtlasRenderTasks.empty()) { |
| 292 |
// args.fPaint should NEVER reference our current atlas. If it does, it means somebody |
| 293 |
// intercepted a clip FP meant for a different op and will cause rendering artifacts. |
| 294 |
const GrSurfaceProxy* atlasProxy = fAtlasRenderTasks.back()->atlasProxy(); |
| 295 |
SkASSERT(!refs_atlas(args.fPaint->getColorFragmentProcessor(), atlasProxy)); |
| 296 |
SkASSERT(!refs_atlas(args.fPaint->getCoverageFragmentProcessor(), atlasProxy)); |
| 297 |
} |
| 298 |
SkASSERT(!args.fHasUserStencilSettings); // See onGetStencilSupport(). |
| 299 |
#endif |
| 300 |
bool canDrawPath = args.fShape->style().isSimpleFill() && |
| 301 |
#ifdef SK_DISABLE_ATLAS_PATH_RENDERER_WITH_COVERAGE_AA |
| 302 |
// The MSAA requirement is a temporary limitation in order to preserve |
| 303 |
// functionality for refactoring. TODO: Allow kCoverage AA types. |
| 304 |
args.fAAType == GrAAType::kMSAA && |
| 305 |
#else |
| 306 |
args.fAAType != GrAAType::kNone && |
| 307 |
#endif |
| 308 |
// Non-DMSAA convex paths should be handled by the convex tessellator. |
| 309 |
// (With DMSAA we continue to use the atlas for these paths in order to avoid |
| 310 |
// triggering MSAA.) |
| 311 |
(args.fProxy->numSamples() == 1 || !args.fShape->knownToBeConvex()) && |
| 312 |
!args.fShape->style().hasPathEffect() && |
| 313 |
!args.fViewMatrix->hasPerspective() && |
| 314 |
this->pathFitsInAtlas(args.fViewMatrix->mapRect(args.fShape->bounds()), |
| 315 |
args.fAAType); |
| 316 |
return canDrawPath ? CanDrawPath::kYes : CanDrawPath::kNo; |
| 317 |
} |
| 318 |
|
| 319 |
bool AtlasPathRenderer::onDrawPath(const DrawPathArgs& args) { |
| 320 |
SkPath path = args.fShape->asPath(); |
| 321 |
|
| 322 |
const SkRect pathDevBounds = args.fViewMatrix->mapRect(args.fShape->bounds()); |
| 323 |
SkASSERT(this->pathFitsInAtlas(pathDevBounds, args.fAAType)); |
| 324 |
|
| 325 |
if (!is_visible(pathDevBounds, args.fClip->getConservativeBounds())) { |
| 326 |
// The path is empty or outside the clip. No mask is needed. |
| 327 |
if (path.isInverseFillType()) { |
| 328 |
args.fSurfaceDrawContext->drawPaint(args.fClip, std::move(args.fPaint), |
| 329 |
*args.fViewMatrix); |
| 330 |
} |
| 331 |
return true; |
| 332 |
} |
| 333 |
|
| 334 |
SkIRect devIBounds; |
| 335 |
SkIPoint16 locationInAtlas; |
| 336 |
bool transposedInAtlas; |
| 337 |
SkAssertResult(this->addPathToAtlas(args.fContext, *args.fViewMatrix, path, pathDevBounds, |
| 338 |
&devIBounds, &locationInAtlas, &transposedInAtlas, |
| 339 |
nullptr/*DrawRefsAtlasCallback -- see onCanDrawPath()*/)); |
| 340 |
|
| 341 |
const SkIRect& fillBounds = args.fShape->inverseFilled() |
| 342 |
? (args.fClip |
| 343 |
? args.fClip->getConservativeBounds() |
| 344 |
: args.fSurfaceDrawContext->asSurfaceProxy()->backingStoreBoundsIRect()) |
| 345 |
: devIBounds; |
| 346 |
const GrCaps& caps = *args.fSurfaceDrawContext->caps(); |
| 347 |
auto op = GrOp::Make<DrawAtlasPathOp>(args.fContext, |
| 348 |
args.fSurfaceDrawContext->arenaAlloc(), |
| 349 |
fillBounds, *args.fViewMatrix, |
| 350 |
std::move(args.fPaint), locationInAtlas, |
| 351 |
devIBounds, transposedInAtlas, |
| 352 |
fAtlasRenderTasks.back()->readView(caps), |
| 353 |
args.fShape->inverseFilled()); |
| 354 |
args.fSurfaceDrawContext->addDrawOp(args.fClip, std::move(op)); |
| 355 |
return true; |
| 356 |
} |
| 357 |
|
| 358 |
GrFPResult AtlasPathRenderer::makeAtlasClipEffect(const SurfaceDrawContext* sdc, |
| 359 |
const GrOp* opBeingClipped, |
| 360 |
std::unique_ptr<GrFragmentProcessor> inputFP, |
| 361 |
const SkIRect& drawBounds, |
| 362 |
const SkMatrix& viewMatrix, |
| 363 |
const SkPath& path) { |
| 364 |
if (viewMatrix.hasPerspective()) { |
| 365 |
return GrFPFailure(std::move(inputFP)); |
| 366 |
} |
| 367 |
|
| 368 |
const SkRect pathDevBounds = viewMatrix.mapRect(path.getBounds()); |
| 369 |
if (!is_visible(pathDevBounds, drawBounds)) { |
| 370 |
// The path is empty or outside the drawBounds. No mask is needed. We explicitly allow the |
| 371 |
// returned successful "fp" to be null in case this bypassed atlas clip effect was the first |
| 372 |
// clip to be processed by the clip stack (at which point inputFP is null). |
| 373 |
return path.isInverseFillType() ? GrFPNullableSuccess(std::move(inputFP)) |
| 374 |
: GrFPFailure(std::move(inputFP)); |
| 375 |
} |
| 376 |
|
| 377 |
auto fallbackAAType = (sdc->numSamples() > 1 || sdc->canUseDynamicMSAA()) ? GrAAType::kMSAA |
| 378 |
: GrAAType::kCoverage; |
| 379 |
if (!this->pathFitsInAtlas(pathDevBounds, fallbackAAType)) { |
| 380 |
// The path is too big. |
| 381 |
return GrFPFailure(std::move(inputFP)); |
| 382 |
} |
| 383 |
|
| 384 |
SkIRect devIBounds; |
| 385 |
SkIPoint16 locationInAtlas; |
| 386 |
bool transposedInAtlas; |
| 387 |
// Called if the atlas runs out of room, to determine if it's safe to create a new one. (Draws |
| 388 |
// can never access more than one atlas.) |
| 389 |
auto drawRefsAtlasCallback = [opBeingClipped, &inputFP](const GrSurfaceProxy* atlasProxy) { |
| 390 |
return refs_atlas(opBeingClipped, atlasProxy) || |
| 391 |
refs_atlas(inputFP.get(), atlasProxy); |
| 392 |
}; |
| 393 |
// addPathToAtlas() ignores inverseness of the fill. See GrAtlasRenderTask::getAtlasUberPath(). |
| 394 |
if (!this->addPathToAtlas(sdc->recordingContext(), viewMatrix, path, pathDevBounds, &devIBounds, |
| 395 |
&locationInAtlas, &transposedInAtlas, drawRefsAtlasCallback)) { |
| 396 |
// The atlas ran out of room and we were unable to start a new one. |
| 397 |
return GrFPFailure(std::move(inputFP)); |
| 398 |
} |
| 399 |
|
| 400 |
SkMatrix atlasMatrix; |
| 401 |
auto [atlasX, atlasY] = locationInAtlas; |
| 402 |
if (!transposedInAtlas) { |
| 403 |
atlasMatrix = SkMatrix::Translate(atlasX - devIBounds.left(), atlasY - devIBounds.top()); |
| 404 |
} else { |
| 405 |
atlasMatrix.setAll(0, 1, atlasX - devIBounds.top(), |
| 406 |
1, 0, atlasY - devIBounds.left(), |
| 407 |
0, 0, 1); |
| 408 |
} |
| 409 |
auto flags = GrModulateAtlasCoverageEffect::Flags::kNone; |
| 410 |
if (path.isInverseFillType()) { |
| 411 |
flags |= GrModulateAtlasCoverageEffect::Flags::kInvertCoverage; |
| 412 |
} |
| 413 |
if (!devIBounds.contains(drawBounds)) { |
| 414 |
flags |= GrModulateAtlasCoverageEffect::Flags::kCheckBounds; |
| 415 |
// At this point in time we expect callers to tighten the scissor for "kIntersect" clips, as |
| 416 |
// opposed to us having to check the path bounds. Feel free to remove this assert if that |
| 417 |
// ever changes. |
| 418 |
SkASSERT(path.isInverseFillType()); |
| 419 |
} |
| 420 |
GrSurfaceProxyView atlasView = fAtlasRenderTasks.back()->readView(*sdc->caps()); |
| 421 |
return GrFPSuccess(std::make_unique<GrModulateAtlasCoverageEffect>(flags, std::move(inputFP), |
| 422 |
std::move(atlasView), |
| 423 |
atlasMatrix, devIBounds)); |
| 424 |
} |
| 425 |
|
| 426 |
bool AtlasPathRenderer::preFlush(GrOnFlushResourceProvider* onFlushRP) { |
| 427 |
if (fAtlasRenderTasks.empty()) { |
| 428 |
SkASSERT(fAtlasPathCache.count() == 0); |
| 429 |
return true; |
| 430 |
} |
| 431 |
|
| 432 |
// Verify the atlases can all share the same texture. |
| 433 |
SkDEBUGCODE(validate_atlas_dependencies(fAtlasRenderTasks);) |
| 434 |
|
| 435 |
bool successful; |
| 436 |
|
| 437 |
#if defined(GPU_TEST_UTILS) |
| 438 |
if (onFlushRP->failFlushTimeCallbacks()) { |
| 439 |
successful = false; |
| 440 |
} else |
| 441 |
#endif |
| 442 |
{ |
| 443 |
// TODO: it seems like this path renderer's backing-texture reuse could be greatly |
| 444 |
// improved. Please see skbug.com/40044379. |
| 445 |
|
| 446 |
// Instantiate the first atlas. |
| 447 |
successful = fAtlasRenderTasks[0]->instantiate(onFlushRP); |
| 448 |
|
| 449 |
// Instantiate the remaining atlases. |
| 450 |
GrTexture* firstAtlas = fAtlasRenderTasks[0]->atlasProxy()->peekTexture(); |
| 451 |
SkASSERT(firstAtlas); |
| 452 |
for (int i = 1; successful && i < fAtlasRenderTasks.size(); ++i) { |
| 453 |
auto atlasTask = fAtlasRenderTasks[i].get(); |
| 454 |
if (atlasTask->atlasProxy()->backingStoreDimensions() == firstAtlas->dimensions()) { |
| 455 |
successful &= atlasTask->instantiate(onFlushRP, sk_ref_sp(firstAtlas)); |
| 456 |
} else { |
| 457 |
// The atlases are expected to all be full size except possibly the final one. |
| 458 |
SkASSERT(i == fAtlasRenderTasks.size() - 1); |
| 459 |
SkASSERT(atlasTask->atlasProxy()->backingStoreDimensions().area() < |
| 460 |
firstAtlas->dimensions().area()); |
| 461 |
// TODO: Recycle the larger atlas texture anyway? |
| 462 |
successful &= atlasTask->instantiate(onFlushRP); |
| 463 |
} |
| 464 |
} |
| 465 |
} |
| 466 |
|
| 467 |
// Reset all atlas data. |
| 468 |
fAtlasRenderTasks.clear(); |
| 469 |
fAtlasPathCache.reset(); |
| 470 |
return successful; |
| 471 |
} |
| 472 |
|
| 473 |
} // namespace skgpu::ganesh |