| 1 |
/* |
| 2 |
* Copyright 2020 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/ClipStack.h" |
| 8 |
|
| 9 |
#include "include/core/SkAlphaType.h" |
| 10 |
#include "include/core/SkBlendMode.h" |
| 11 |
#include "include/core/SkClipOp.h" |
| 12 |
#include "include/core/SkColorSpace.h" |
| 13 |
#include "include/core/SkMatrix.h" |
| 14 |
#include "include/core/SkPath.h" |
| 15 |
#include "include/core/SkRRect.h" |
| 16 |
#include "include/core/SkRegion.h" |
| 17 |
#include "include/core/SkSamplingOptions.h" |
| 18 |
#include "include/core/SkScalar.h" |
| 19 |
#include "include/gpu/GpuTypes.h" |
| 20 |
#include "include/gpu/ganesh/GrBackendSurface.h" |
| 21 |
#include "include/gpu/ganesh/GrDirectContext.h" |
| 22 |
#include "include/gpu/ganesh/GrRecordingContext.h" |
| 23 |
#include "include/gpu/ganesh/GrTypes.h" |
| 24 |
#include "include/private/base/SkPoint_impl.h" |
| 25 |
#include "include/private/base/SkTArray.h" |
| 26 |
#include "include/private/base/SkTo.h" |
| 27 |
#include "include/private/gpu/ganesh/GrTypesPriv.h" |
| 28 |
#include "src/base/SkVx.h" |
| 29 |
#include "src/core/SkPathPriv.h" |
| 30 |
#include "src/core/SkRRectPriv.h" |
| 31 |
#include "src/core/SkRectPriv.h" |
| 32 |
#include "src/core/SkTaskGroup.h" |
| 33 |
#include "src/core/SkTraceEvent.h" |
| 34 |
#include "src/gpu/SkBackingFit.h" |
| 35 |
#include "src/gpu/Swizzle.h" |
| 36 |
#include "src/gpu/ganesh/GrAppliedClip.h" |
| 37 |
#include "src/gpu/ganesh/GrCaps.h" |
| 38 |
#include "src/gpu/ganesh/GrClip.h" |
| 39 |
#include "src/gpu/ganesh/GrColorInfo.h" |
| 40 |
#include "src/gpu/ganesh/GrDeferredProxyUploader.h" |
| 41 |
#include "src/gpu/ganesh/GrDirectContextPriv.h" |
| 42 |
#include "src/gpu/ganesh/GrDrawingManager.h" |
| 43 |
#include "src/gpu/ganesh/GrFPArgs.h" |
| 44 |
#include "src/gpu/ganesh/GrFragmentProcessor.h" |
| 45 |
#include "src/gpu/ganesh/GrFragmentProcessors.h" |
| 46 |
#include "src/gpu/ganesh/GrProxyProvider.h" |
| 47 |
#include "src/gpu/ganesh/GrRecordingContextPriv.h" |
| 48 |
#include "src/gpu/ganesh/GrRenderTargetProxy.h" |
| 49 |
#include "src/gpu/ganesh/GrSWMaskHelper.h" |
| 50 |
#include "src/gpu/ganesh/GrSamplerState.h" |
| 51 |
#include "src/gpu/ganesh/GrSurfaceProxy.h" |
| 52 |
#include "src/gpu/ganesh/GrSurfaceProxyView.h" |
| 53 |
#include "src/gpu/ganesh/GrTextureProxy.h" |
| 54 |
#include "src/gpu/ganesh/GrTextureProxyPriv.h" |
| 55 |
#include "src/gpu/ganesh/GrWindowRectangles.h" |
| 56 |
#include "src/gpu/ganesh/GrWindowRectsState.h" |
| 57 |
#include "src/gpu/ganesh/StencilMaskHelper.h" |
| 58 |
#include "src/gpu/ganesh/SurfaceDrawContext.h" |
| 59 |
#include "src/gpu/ganesh/effects/GrBlendFragmentProcessor.h" |
| 60 |
#include "src/gpu/ganesh/effects/GrConvexPolyEffect.h" |
| 61 |
#include "src/gpu/ganesh/effects/GrRRectEffect.h" |
| 62 |
#include "src/gpu/ganesh/effects/GrTextureEffect.h" |
| 63 |
#include "src/gpu/ganesh/geometry/GrQuad.h" |
| 64 |
#include "src/gpu/ganesh/geometry/GrQuadUtils.h" |
| 65 |
#include "src/gpu/ganesh/ops/AtlasPathRenderer.h" |
| 66 |
#include "src/gpu/ganesh/ops/GrDrawOp.h" |
| 67 |
|
| 68 |
#include <algorithm> |
| 69 |
#include <atomic> |
| 70 |
#include <functional> |
| 71 |
#include <tuple> |
| 72 |
#include <utility> |
| 73 |
|
| 74 |
class GrOp; |
| 75 |
struct GrShaderCaps; |
| 76 |
|
| 77 |
using namespace skia_private; |
| 78 |
|
| 79 |
namespace { |
| 80 |
|
| 81 |
// This captures which of the two elements in (A op B) would be required when they are combined, |
| 82 |
// where op is intersect or difference. |
| 83 |
enum class ClipGeometry { |
| 84 |
kEmpty, |
| 85 |
kAOnly, |
| 86 |
kBOnly, |
| 87 |
kBoth |
| 88 |
}; |
| 89 |
|
| 90 |
// A and B can be Element, SaveRecord, or Draw. Supported combinations are, order not mattering, |
| 91 |
// (Element, Element), (Element, SaveRecord), (Element, Draw), and (SaveRecord, Draw). |
| 92 |
template<typename A, typename B> |
| 93 |
ClipGeometry get_clip_geometry(const A& a, const B& b) { |
| 94 |
// NOTE: SkIRect::Intersects() returns false when two rectangles touch at an edge (so the result |
| 95 |
// is empty). This behavior is desired for the following clip effect policies. |
| 96 |
if (a.op() == SkClipOp::kIntersect) { |
| 97 |
if (b.op() == SkClipOp::kIntersect) { |
| 98 |
// Intersect (A) + Intersect (B) |
| 99 |
if (!SkIRect::Intersects(a.outerBounds(), b.outerBounds())) { |
| 100 |
// Regions with non-zero coverage are disjoint, so intersection = empty |
| 101 |
return ClipGeometry::kEmpty; |
| 102 |
} else if (b.contains(a)) { |
| 103 |
// B's full coverage region contains entirety of A, so intersection = A |
| 104 |
return ClipGeometry::kAOnly; |
| 105 |
} else if (a.contains(b)) { |
| 106 |
// A's full coverage region contains entirety of B, so intersection = B |
| 107 |
return ClipGeometry::kBOnly; |
| 108 |
} else { |
| 109 |
// The shapes intersect in some non-trivial manner |
| 110 |
return ClipGeometry::kBoth; |
| 111 |
} |
| 112 |
} else { |
| 113 |
SkASSERT(b.op() == SkClipOp::kDifference); |
| 114 |
// Intersect (A) + Difference (B) |
| 115 |
if (!SkIRect::Intersects(a.outerBounds(), b.outerBounds())) { |
| 116 |
// A only intersects B's full coverage region, so intersection = A |
| 117 |
return ClipGeometry::kAOnly; |
| 118 |
} else if (b.contains(a)) { |
| 119 |
// B's zero coverage region completely contains A, so intersection = empty |
| 120 |
return ClipGeometry::kEmpty; |
| 121 |
} else { |
| 122 |
// Intersection cannot be simplified. Note that the combination of a intersect |
| 123 |
// and difference op in this order cannot produce kBOnly |
| 124 |
return ClipGeometry::kBoth; |
| 125 |
} |
| 126 |
} |
| 127 |
} else { |
| 128 |
SkASSERT(a.op() == SkClipOp::kDifference); |
| 129 |
if (b.op() == SkClipOp::kIntersect) { |
| 130 |
// Difference (A) + Intersect (B) - the mirror of Intersect(A) + Difference(B), |
| 131 |
// but combining is commutative so this is equivalent barring naming. |
| 132 |
if (!SkIRect::Intersects(b.outerBounds(), a.outerBounds())) { |
| 133 |
// B only intersects A's full coverage region, so intersection = B |
| 134 |
return ClipGeometry::kBOnly; |
| 135 |
} else if (a.contains(b)) { |
| 136 |
// A's zero coverage region completely contains B, so intersection = empty |
| 137 |
return ClipGeometry::kEmpty; |
| 138 |
} else { |
| 139 |
// Cannot be simplified |
| 140 |
return ClipGeometry::kBoth; |
| 141 |
} |
| 142 |
} else { |
| 143 |
SkASSERT(b.op() == SkClipOp::kDifference); |
| 144 |
// Difference (A) + Difference (B) |
| 145 |
if (a.contains(b)) { |
| 146 |
// A's zero coverage region contains B, so B doesn't remove any extra |
| 147 |
// coverage from their intersection. |
| 148 |
return ClipGeometry::kAOnly; |
| 149 |
} else if (b.contains(a)) { |
| 150 |
// Mirror of the above case, intersection = B instead |
| 151 |
return ClipGeometry::kBOnly; |
| 152 |
} else { |
| 153 |
// Intersection of the two differences cannot be simplified. Note that for |
| 154 |
// this op combination it is not possible to produce kEmpty. |
| 155 |
return ClipGeometry::kBoth; |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
// a.contains(b) where a's local space is defined by 'aToDevice', and b's possibly separate local |
| 162 |
// space is defined by 'bToDevice'. 'a' and 'b' geometry are provided in their local spaces. |
| 163 |
// Automatically takes into account if the anti-aliasing policies differ. When the policies match, |
| 164 |
// we assume that coverage AA or GPU's non-AA rasterization will apply to A and B equivalently, so |
| 165 |
// we can compare the original shapes. When the modes are mixed, we outset B in device space first. |
| 166 |
bool shape_contains_rect(const GrShape& a, const SkMatrix& aToDevice, const SkMatrix& deviceToA, |
| 167 |
const SkRect& b, const SkMatrix& bToDevice, bool mixedAAMode) { |
| 168 |
if (!a.convex()) { |
| 169 |
return false; |
| 170 |
} |
| 171 |
|
| 172 |
if (!mixedAAMode && aToDevice == bToDevice) { |
| 173 |
// A and B are in the same coordinate space, so don't bother mapping |
| 174 |
return a.conservativeContains(b); |
| 175 |
} else if (bToDevice.isIdentity() && aToDevice.preservesAxisAlignment()) { |
| 176 |
// Optimize the common case of draws (B, with identity matrix) and axis-aligned shapes, |
| 177 |
// instead of checking the four corners separately. |
| 178 |
SkRect bInA = b; |
| 179 |
if (mixedAAMode) { |
| 180 |
bInA.outset(0.5f, 0.5f); |
| 181 |
} |
| 182 |
SkAssertResult(deviceToA.mapRect(&bInA)); |
| 183 |
return a.conservativeContains(bInA); |
| 184 |
} |
| 185 |
|
| 186 |
// Test each corner for contains; since a is convex, if all 4 corners of b's bounds are |
| 187 |
// contained, then the entirety of b is within a. |
| 188 |
GrQuad deviceQuad = GrQuad::MakeFromRect(b, bToDevice); |
| 189 |
if (mixedAAMode) { |
| 190 |
// Outset it so its edges are 1/2px out, giving us a buffer to avoid cases where a non-AA |
| 191 |
// clip or draw would snap outside an aa element. |
| 192 |
GrQuadUtils::Outset({0.5f, 0.5f, 0.5f, 0.5f}, &deviceQuad); |
| 193 |
} |
| 194 |
if (any(deviceQuad.w4f() < SkPathPriv::kW0PlaneDistance)) { |
| 195 |
// Something in B actually projects behind the W = 0 plane and would be clipped to infinity, |
| 196 |
// so it's extremely unlikely that A can contain B. |
| 197 |
return false; |
| 198 |
} |
| 199 |
|
| 200 |
for (int i = 0; i < 4; ++i) { |
| 201 |
SkPoint cornerInA = deviceToA.mapPoint(deviceQuad.point(i)); |
| 202 |
if (!a.conservativeContains(cornerInA)) { |
| 203 |
return false; |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
return true; |
| 208 |
} |
| 209 |
|
| 210 |
SkIRect subtract(const SkIRect& a, const SkIRect& b, bool exact) { |
| 211 |
SkIRect diff; |
| 212 |
if (SkRectPriv::Subtract(a, b, &diff) || !exact) { |
| 213 |
// Either A-B is exactly the rectangle stored in diff, or we don't need an exact answer |
| 214 |
// and can settle for the subrect of A excluded from B (which is also 'diff') |
| 215 |
return diff; |
| 216 |
} else { |
| 217 |
// For our purposes, we want the original A when A-B cannot be exactly represented |
| 218 |
return a; |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
GrClipEdgeType get_clip_edge_type(SkClipOp op, GrAA aa) { |
| 223 |
if (op == SkClipOp::kIntersect) { |
| 224 |
return aa == GrAA::kYes ? GrClipEdgeType::kFillAA : GrClipEdgeType::kFillBW; |
| 225 |
} else { |
| 226 |
return aa == GrAA::kYes ? GrClipEdgeType::kInverseFillAA : GrClipEdgeType::kInverseFillBW; |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
static uint32_t kInvalidGenID = 0; |
| 231 |
static uint32_t kEmptyGenID = 1; |
| 232 |
static uint32_t kWideOpenGenID = 2; |
| 233 |
|
| 234 |
uint32_t next_gen_id() { |
| 235 |
// 0-2 are reserved for invalid, empty & wide-open |
| 236 |
static const uint32_t kFirstUnreservedGenID = 3; |
| 237 |
static std::atomic<uint32_t> nextID{kFirstUnreservedGenID}; |
| 238 |
|
| 239 |
uint32_t id; |
| 240 |
do { |
| 241 |
id = nextID.fetch_add(1, std::memory_order_relaxed); |
| 242 |
} while (id < kFirstUnreservedGenID); |
| 243 |
return id; |
| 244 |
} |
| 245 |
|
| 246 |
// Functions for rendering / applying clip shapes in various ways |
| 247 |
// The general strategy is: |
| 248 |
// - Represent the clip element as an analytic FP that tests sk_FragCoord vs. its device shape |
| 249 |
// - Render the clip element to the stencil, if stencil is allowed and supports the AA, and the |
| 250 |
// size of the element indicates stenciling will be worth it, vs. making a mask. |
| 251 |
// - Try to put the individual element into a clip atlas, which is then sampled during the draw |
| 252 |
// - Render the element into a SW mask and upload it. If possible, the SW rasterization happens |
| 253 |
// in parallel. |
| 254 |
static constexpr GrSurfaceOrigin kMaskOrigin = kTopLeft_GrSurfaceOrigin; |
| 255 |
|
| 256 |
GrFPResult analytic_clip_fp(const skgpu::ganesh::ClipStack::Element& e, |
| 257 |
const GrShaderCaps& caps, |
| 258 |
std::unique_ptr<GrFragmentProcessor> fp) { |
| 259 |
// All analytic clip shape FPs need to be in device space |
| 260 |
GrClipEdgeType edgeType = get_clip_edge_type(e.fOp, e.fAA); |
| 261 |
if (e.fLocalToDevice.isIdentity()) { |
| 262 |
if (e.fShape.isRect()) { |
| 263 |
return GrFPSuccess(GrFragmentProcessor::Rect(std::move(fp), edgeType, e.fShape.rect())); |
| 264 |
} else if (e.fShape.isRRect()) { |
| 265 |
return GrRRectEffect::Make(std::move(fp), edgeType, e.fShape.rrect(), caps); |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
// A convex hull can be transformed into device space (this will handle rect shapes with a |
| 270 |
// non-identity transform). |
| 271 |
if (e.fShape.segmentMask() == SkPath::kLine_SegmentMask && e.fShape.convex()) { |
| 272 |
SkPath devicePath = e.fShape.asPath().makeTransform(e.fLocalToDevice); |
| 273 |
return GrConvexPolyEffect::Make(std::move(fp), edgeType, devicePath); |
| 274 |
} |
| 275 |
|
| 276 |
return GrFPFailure(std::move(fp)); |
| 277 |
} |
| 278 |
|
| 279 |
// TODO: Currently this only works with tessellation because the tessellation path renderer owns and |
| 280 |
// manages the atlas. The high-level concept could be generalized to support any path renderer going |
| 281 |
// into a shared atlas. |
| 282 |
GrFPResult clip_atlas_fp(const skgpu::ganesh::SurfaceDrawContext* sdc, |
| 283 |
const GrOp* opBeingClipped, |
| 284 |
skgpu::ganesh::AtlasPathRenderer* atlasPathRenderer, |
| 285 |
const SkIRect& scissorBounds, |
| 286 |
const skgpu::ganesh::ClipStack::Element& e, |
| 287 |
std::unique_ptr<GrFragmentProcessor> inputFP) { |
| 288 |
if (e.fAA != GrAA::kYes) { |
| 289 |
return GrFPFailure(std::move(inputFP)); |
| 290 |
} |
| 291 |
SkPath path = e.fShape.asPath(); |
| 292 |
SkASSERT(!path.isInverseFillType()); |
| 293 |
if (e.fOp == SkClipOp::kDifference) { |
| 294 |
// Toggling fill type does not affect the path's "generationID" key. |
| 295 |
path.toggleInverseFillType(); |
| 296 |
} |
| 297 |
return atlasPathRenderer->makeAtlasClipEffect(sdc, opBeingClipped, std::move(inputFP), |
| 298 |
scissorBounds, e.fLocalToDevice, path); |
| 299 |
} |
| 300 |
|
| 301 |
void draw_to_sw_mask(GrSWMaskHelper* helper, |
| 302 |
const skgpu::ganesh::ClipStack::Element& e, |
| 303 |
bool clearMask) { |
| 304 |
// If the first element to draw is an intersect, we clear to 0 and will draw it directly with |
| 305 |
// coverage 1 (subsequent intersect elements will be inverse-filled and draw 0 outside). |
| 306 |
// If the first element to draw is a difference, we clear to 1, and in all cases we draw the |
| 307 |
// difference element directly with coverage 0. |
| 308 |
if (clearMask) { |
| 309 |
helper->clear(e.fOp == SkClipOp::kIntersect ? 0x00 : 0xFF); |
| 310 |
} |
| 311 |
|
| 312 |
uint8_t alpha; |
| 313 |
bool invert; |
| 314 |
if (e.fOp == SkClipOp::kIntersect) { |
| 315 |
// Intersect modifies pixels outside of its geometry. If this isn't the first op, we |
| 316 |
// draw the inverse-filled shape with 0 coverage to erase everything outside the element |
| 317 |
// But if we are the first element, we can draw directly with coverage 1 since we |
| 318 |
// cleared to 0. |
| 319 |
if (clearMask) { |
| 320 |
alpha = 0xFF; |
| 321 |
invert = false; |
| 322 |
} else { |
| 323 |
alpha = 0x00; |
| 324 |
invert = true; |
| 325 |
} |
| 326 |
} else { |
| 327 |
// For difference ops, can always just subtract the shape directly by drawing 0 coverage |
| 328 |
SkASSERT(e.fOp == SkClipOp::kDifference); |
| 329 |
alpha = 0x00; |
| 330 |
invert = false; |
| 331 |
} |
| 332 |
|
| 333 |
// Draw the shape; based on how we've initialized the buffer and chosen alpha+invert, |
| 334 |
// every element is drawn with the kReplace_Op |
| 335 |
if (invert) { |
| 336 |
// Must invert the path |
| 337 |
SkASSERT(!e.fShape.inverted()); |
| 338 |
// TODO: this is an extra copy effectively, just so we can toggle inversion; would be |
| 339 |
// better perhaps to just call a drawPath() since we know it'll use path rendering w/ |
| 340 |
// the inverse fill type. |
| 341 |
GrShape inverted(e.fShape); |
| 342 |
inverted.setInverted(true); |
| 343 |
helper->drawShape(inverted, e.fLocalToDevice, e.fAA, alpha); |
| 344 |
} else { |
| 345 |
helper->drawShape(e.fShape, e.fLocalToDevice, e.fAA, alpha); |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
GrSurfaceProxyView render_sw_mask(GrRecordingContext* context, |
| 350 |
const SkIRect& bounds, |
| 351 |
const skgpu::ganesh::ClipStack::Element** elements, |
| 352 |
int count) { |
| 353 |
SkASSERT(count > 0); |
| 354 |
|
| 355 |
SkTaskGroup* taskGroup = nullptr; |
| 356 |
if (auto direct = context->asDirectContext()) { |
| 357 |
taskGroup = direct->priv().getTaskGroup(); |
| 358 |
} |
| 359 |
|
| 360 |
if (taskGroup) { |
| 361 |
const GrCaps* caps = context->priv().caps(); |
| 362 |
GrProxyProvider* proxyProvider = context->priv().proxyProvider(); |
| 363 |
|
| 364 |
// Create our texture proxy |
| 365 |
GrBackendFormat format = caps->getDefaultBackendFormat(GrColorType::kAlpha_8, |
| 366 |
GrRenderable::kNo); |
| 367 |
|
| 368 |
skgpu::Swizzle swizzle = context->priv().caps()->getReadSwizzle(format, |
| 369 |
GrColorType::kAlpha_8); |
| 370 |
auto proxy = proxyProvider->createProxy(format, |
| 371 |
bounds.size(), |
| 372 |
GrRenderable::kNo, |
| 373 |
1, |
| 374 |
skgpu::Mipmapped::kNo, |
| 375 |
SkBackingFit::kApprox, |
| 376 |
skgpu::Budgeted::kYes, |
| 377 |
GrProtected::kNo, |
| 378 |
/*label=*/"ClipStack_RenderSwMask"); |
| 379 |
|
| 380 |
// Since this will be rendered on another thread, make a copy of the elements in case |
| 381 |
// the clip stack is modified on the main thread |
| 382 |
using Uploader = GrTDeferredProxyUploader<TArray<skgpu::ganesh::ClipStack::Element>>; |
| 383 |
std::unique_ptr<Uploader> uploader = std::make_unique<Uploader>(count); |
| 384 |
for (int i = 0; i < count; ++i) { |
| 385 |
uploader->data().push_back(*(elements[i])); |
| 386 |
} |
| 387 |
|
| 388 |
Uploader* uploaderRaw = uploader.get(); |
| 389 |
auto drawAndUploadMask = [uploaderRaw, bounds] { |
| 390 |
TRACE_EVENT0("skia.gpu", "Threaded SW Clip Mask Render"); |
| 391 |
GrSWMaskHelper helper(uploaderRaw->getPixels()); |
| 392 |
if (helper.init(bounds)) { |
| 393 |
for (int i = 0; i < uploaderRaw->data().size(); ++i) { |
| 394 |
draw_to_sw_mask(&helper, uploaderRaw->data()[i], i == 0); |
| 395 |
} |
| 396 |
} else { |
| 397 |
SkDEBUGFAIL("Unable to allocate SW clip mask."); |
| 398 |
} |
| 399 |
uploaderRaw->signalAndFreeData(); |
| 400 |
}; |
| 401 |
|
| 402 |
taskGroup->add(std::move(drawAndUploadMask)); |
| 403 |
proxy->texPriv().setDeferredUploader(std::move(uploader)); |
| 404 |
|
| 405 |
return {std::move(proxy), kMaskOrigin, swizzle}; |
| 406 |
} else { |
| 407 |
GrSWMaskHelper helper; |
| 408 |
if (!helper.init(bounds)) { |
| 409 |
return {}; |
| 410 |
} |
| 411 |
|
| 412 |
for (int i = 0; i < count; ++i) { |
| 413 |
draw_to_sw_mask(&helper,*(elements[i]), i == 0); |
| 414 |
} |
| 415 |
|
| 416 |
return helper.toTextureView(context, SkBackingFit::kApprox); |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
void render_stencil_mask(GrRecordingContext* rContext, |
| 421 |
skgpu::ganesh::SurfaceDrawContext* sdc, |
| 422 |
uint32_t genID, |
| 423 |
const SkIRect& bounds, |
| 424 |
const skgpu::ganesh::ClipStack::Element** elements, |
| 425 |
int count, |
| 426 |
GrAppliedClip* out) { |
| 427 |
skgpu::ganesh::StencilMaskHelper helper(rContext, sdc); |
| 428 |
if (helper.init(bounds, genID, out->windowRectsState().windows(), 0)) { |
| 429 |
// This follows the same logic as in draw_sw_mask |
| 430 |
bool startInside = elements[0]->fOp == SkClipOp::kDifference; |
| 431 |
helper.clear(startInside); |
| 432 |
for (int i = 0; i < count; ++i) { |
| 433 |
const skgpu::ganesh::ClipStack::Element& e = *(elements[i]); |
| 434 |
SkRegion::Op op; |
| 435 |
if (e.fOp == SkClipOp::kIntersect) { |
| 436 |
op = (i == 0) ? SkRegion::kReplace_Op : SkRegion::kIntersect_Op; |
| 437 |
} else { |
| 438 |
op = SkRegion::kDifference_Op; |
| 439 |
} |
| 440 |
helper.drawShape(e.fShape, e.fLocalToDevice, op, e.fAA); |
| 441 |
} |
| 442 |
helper.finish(); |
| 443 |
} |
| 444 |
out->hardClip().addStencilClip(genID); |
| 445 |
} |
| 446 |
|
| 447 |
} // anonymous namespace |
| 448 |
|
| 449 |
namespace skgpu::ganesh { |
| 450 |
|
| 451 |
class ClipStack::Draw { |
| 452 |
public: |
| 453 |
Draw(const SkRect& drawBounds, GrAA aa) |
| 454 |
: fBounds(GrClip::GetPixelIBounds(drawBounds, aa, BoundsType::kExterior)) |
| 455 |
, fAA(aa) { |
| 456 |
// Be slightly more forgiving on whether or not a draw is inside a clip element. |
| 457 |
fOriginalBounds = drawBounds.makeInset(GrClip::kBoundsTolerance, GrClip::kBoundsTolerance); |
| 458 |
if (fOriginalBounds.isEmpty()) { |
| 459 |
fOriginalBounds = drawBounds; |
| 460 |
} |
| 461 |
} |
| 462 |
|
| 463 |
// Common clip type interface |
| 464 |
SkClipOp op() const { return SkClipOp::kIntersect; } |
| 465 |
const SkIRect& outerBounds() const { return fBounds; } |
| 466 |
|
| 467 |
// Draw does not have inner bounds so cannot contain anything. |
| 468 |
bool contains(const RawElement& e) const { return false; } |
| 469 |
bool contains(const SaveRecord& s) const { return false; } |
| 470 |
|
| 471 |
bool applyDeviceBounds(const SkIRect& deviceBounds) { |
| 472 |
return fBounds.intersect(deviceBounds); |
| 473 |
} |
| 474 |
|
| 475 |
const SkRect& bounds() const { return fOriginalBounds; } |
| 476 |
GrAA aa() const { return fAA; } |
| 477 |
|
| 478 |
private: |
| 479 |
SkRect fOriginalBounds; |
| 480 |
SkIRect fBounds; |
| 481 |
GrAA fAA; |
| 482 |
}; |
| 483 |
|
| 484 |
/////////////////////////////////////////////////////////////////////////////// |
| 485 |
// ClipStack::Element |
| 486 |
|
| 487 |
ClipStack::RawElement::RawElement(const SkMatrix& localToDevice, const GrShape& shape, |
| 488 |
GrAA aa, SkClipOp op) |
| 489 |
: Element{shape, localToDevice, op, aa} |
| 490 |
, fInnerBounds(SkIRect::MakeEmpty()) |
| 491 |
, fOuterBounds(SkIRect::MakeEmpty()) |
| 492 |
, fInvalidatedByIndex(-1) { |
| 493 |
if (auto inv = localToDevice.invert()) { |
| 494 |
fDeviceToLocal = *inv; |
| 495 |
} else { |
| 496 |
// If the transform can't be inverted, it means that two dimensions are collapsed to 0 or |
| 497 |
// 1 dimension, making the device-space geometry effectively empty. |
| 498 |
fShape.reset(); |
| 499 |
} |
| 500 |
} |
| 501 |
|
| 502 |
void ClipStack::RawElement::markInvalid(const SaveRecord& current) { |
| 503 |
SkASSERT(!this->isInvalid()); |
| 504 |
fInvalidatedByIndex = current.firstActiveElementIndex(); |
| 505 |
} |
| 506 |
|
| 507 |
void ClipStack::RawElement::restoreValid(const SaveRecord& current) { |
| 508 |
if (current.firstActiveElementIndex() < fInvalidatedByIndex) { |
| 509 |
fInvalidatedByIndex = -1; |
| 510 |
} |
| 511 |
} |
| 512 |
|
| 513 |
bool ClipStack::RawElement::contains(const Draw& d) const { |
| 514 |
if (fInnerBounds.contains(d.outerBounds())) { |
| 515 |
return true; |
| 516 |
} else { |
| 517 |
// If the draw is non-AA, use the already computed outer bounds so we don't need to use |
| 518 |
// device-space outsetting inside shape_contains_rect. |
| 519 |
SkRect queryBounds = d.aa() == GrAA::kYes ? d.bounds() : SkRect::Make(d.outerBounds()); |
| 520 |
return shape_contains_rect(fShape, fLocalToDevice, fDeviceToLocal, |
| 521 |
queryBounds, SkMatrix::I(), /* mixed-aa */ false); |
| 522 |
} |
| 523 |
} |
| 524 |
|
| 525 |
bool ClipStack::RawElement::contains(const SaveRecord& s) const { |
| 526 |
if (fInnerBounds.contains(s.outerBounds())) { |
| 527 |
return true; |
| 528 |
} else { |
| 529 |
// This is very similar to contains(Draw) but we just have outerBounds to work with. |
| 530 |
SkRect queryBounds = SkRect::Make(s.outerBounds()); |
| 531 |
return shape_contains_rect(fShape, fLocalToDevice, fDeviceToLocal, |
| 532 |
queryBounds, SkMatrix::I(), /* mixed-aa */ false); |
| 533 |
} |
| 534 |
} |
| 535 |
|
| 536 |
bool ClipStack::RawElement::contains(const RawElement& e) const { |
| 537 |
// This is similar to how RawElement checks containment for a Draw, except that both the tester |
| 538 |
// and testee have a transform that needs to be considered. |
| 539 |
if (fInnerBounds.contains(e.fOuterBounds)) { |
| 540 |
return true; |
| 541 |
} |
| 542 |
|
| 543 |
bool mixedAA = fAA != e.fAA; |
| 544 |
if (!mixedAA && fLocalToDevice == e.fLocalToDevice) { |
| 545 |
// Test the shapes directly against each other, with a special check for a rrect+rrect |
| 546 |
// containment (a intersect b == a implies b contains a) and paths (same gen ID, or same |
| 547 |
// path for small paths means they contain each other). |
| 548 |
static constexpr int kMaxPathComparePoints = 16; |
| 549 |
if (fShape.isRRect() && e.fShape.isRRect()) { |
| 550 |
return SkRRectPriv::ConservativeIntersect(fShape.rrect(), e.fShape.rrect()) |
| 551 |
== e.fShape.rrect(); |
| 552 |
} else if (fShape.isPath() && e.fShape.isPath()) { |
| 553 |
return fShape.path().getGenerationID() == e.fShape.path().getGenerationID() || |
| 554 |
(fShape.path().countPoints() <= kMaxPathComparePoints && |
| 555 |
fShape.path() == e.fShape.path()); |
| 556 |
} // else fall through to shape_contains_rect |
| 557 |
} |
| 558 |
|
| 559 |
return shape_contains_rect(fShape, fLocalToDevice, fDeviceToLocal, |
| 560 |
e.fShape.bounds(), e.fLocalToDevice, mixedAA); |
| 561 |
|
| 562 |
} |
| 563 |
|
| 564 |
void ClipStack::RawElement::simplify(const SkIRect& deviceBounds, bool forceAA) { |
| 565 |
// Make sure the shape is not inverted. An inverted shape is equivalent to a non-inverted shape |
| 566 |
// with the clip op toggled. |
| 567 |
if (fShape.inverted()) { |
| 568 |
fOp = fOp == SkClipOp::kIntersect ? SkClipOp::kDifference : SkClipOp::kIntersect; |
| 569 |
fShape.setInverted(false); |
| 570 |
} |
| 571 |
|
| 572 |
// Then simplify the base shape, if it becomes empty, no need to update the bounds |
| 573 |
fShape.simplify(); |
| 574 |
SkASSERT(!fShape.inverted()); |
| 575 |
if (fShape.isEmpty()) { |
| 576 |
return; |
| 577 |
} |
| 578 |
|
| 579 |
// Lines and points should have been turned into empty since we assume everything is filled |
| 580 |
SkASSERT(!fShape.isPoint() && !fShape.isLine()); |
| 581 |
// Validity check, we have no public API to create an arc at the moment |
| 582 |
SkASSERT(!fShape.isArc()); |
| 583 |
|
| 584 |
SkRect outer = fLocalToDevice.mapRect(fShape.bounds()); |
| 585 |
if (!outer.intersect(SkRect::Make(deviceBounds))) { |
| 586 |
// A non-empty shape is offscreen, so treat it as empty |
| 587 |
fShape.reset(); |
| 588 |
return; |
| 589 |
} |
| 590 |
|
| 591 |
// Except for axis-aligned clip rects, upgrade to AA when forced. We skip axis-aligned clip |
| 592 |
// rects because a non-AA axis aligned rect can always be set as just a scissor test or window |
| 593 |
// rect, avoiding an expensive stencil mask generation. |
| 594 |
if (forceAA && !(fShape.isRect() && fLocalToDevice.preservesAxisAlignment())) { |
| 595 |
fAA = GrAA::kYes; |
| 596 |
} |
| 597 |
|
| 598 |
// Except for non-AA axis-aligned rects, the outer bounds is the rounded-out device-space |
| 599 |
// mapped bounds of the shape. |
| 600 |
fOuterBounds = GrClip::GetPixelIBounds(outer, fAA, BoundsType::kExterior); |
| 601 |
|
| 602 |
if (fLocalToDevice.preservesAxisAlignment()) { |
| 603 |
if (fShape.isRect()) { |
| 604 |
// The actual geometry can be updated to the device-intersected bounds and we can |
| 605 |
// know the inner bounds |
| 606 |
fShape.rect() = outer; |
| 607 |
fLocalToDevice.setIdentity(); |
| 608 |
fDeviceToLocal.setIdentity(); |
| 609 |
|
| 610 |
if (fAA == GrAA::kNo && outer.width() >= 1.f && outer.height() >= 1.f) { |
| 611 |
// NOTE: Legacy behavior to avoid performance regressions. For non-aa axis-aligned |
| 612 |
// clip rects we always just round so that they can be scissor-only (avoiding the |
| 613 |
// uncertainty in how a GPU might actually round an edge on fractional coords). |
| 614 |
fOuterBounds = outer.round(); |
| 615 |
fInnerBounds = fOuterBounds; |
| 616 |
} else { |
| 617 |
fInnerBounds = GrClip::GetPixelIBounds(outer, fAA, BoundsType::kInterior); |
| 618 |
SkASSERT(fOuterBounds.contains(fInnerBounds) || fInnerBounds.isEmpty()); |
| 619 |
} |
| 620 |
} else if (fShape.isRRect()) { |
| 621 |
// Can't transform in place and must still check transform result since some very |
| 622 |
// ill-formed scale+translate matrices can cause invalid rrect radii. |
| 623 |
if (auto rr = fShape.rrect().transform(fLocalToDevice)) { |
| 624 |
fShape.rrect() = *rr; |
| 625 |
fLocalToDevice.setIdentity(); |
| 626 |
fDeviceToLocal.setIdentity(); |
| 627 |
|
| 628 |
SkRect inner = SkRRectPriv::InnerBounds(fShape.rrect()); |
| 629 |
fInnerBounds = GrClip::GetPixelIBounds(inner, fAA, BoundsType::kInterior); |
| 630 |
if (!fInnerBounds.intersect(deviceBounds)) { |
| 631 |
fInnerBounds = SkIRect::MakeEmpty(); |
| 632 |
} |
| 633 |
} |
| 634 |
} |
| 635 |
} |
| 636 |
|
| 637 |
if (fOuterBounds.isEmpty()) { |
| 638 |
// This can happen if we have non-AA shapes smaller than a pixel that do not cover a pixel |
| 639 |
// center. We could round out, but rasterization would still result in an empty clip. |
| 640 |
fShape.reset(); |
| 641 |
} |
| 642 |
|
| 643 |
// Post-conditions on inner and outer bounds |
| 644 |
SkASSERT(fShape.isEmpty() || (!fOuterBounds.isEmpty() && deviceBounds.contains(fOuterBounds))); |
| 645 |
SkASSERT(fShape.isEmpty() || fInnerBounds.isEmpty() || fOuterBounds.contains(fInnerBounds)); |
| 646 |
} |
| 647 |
|
| 648 |
bool ClipStack::RawElement::combine(const RawElement& other, const SaveRecord& current) { |
| 649 |
// To reduce the number of possibilities, only consider intersect+intersect. Difference and |
| 650 |
// mixed op cases could be analyzed to simplify one of the shapes, but that is a rare |
| 651 |
// occurrence and the math is much more complicated. |
| 652 |
if (other.fOp != SkClipOp::kIntersect || fOp != SkClipOp::kIntersect) { |
| 653 |
return false; |
| 654 |
} |
| 655 |
|
| 656 |
// At the moment, only rect+rect or rrect+rrect are supported (although rect+rrect is |
| 657 |
// treated as a degenerate case of rrect+rrect). |
| 658 |
bool shapeUpdated = false; |
| 659 |
if (fShape.isRect() && other.fShape.isRect()) { |
| 660 |
bool aaMatch = fAA == other.fAA; |
| 661 |
if (fLocalToDevice.isIdentity() && other.fLocalToDevice.isIdentity() && !aaMatch) { |
| 662 |
if (GrClip::IsPixelAligned(fShape.rect())) { |
| 663 |
// Our AA type doesn't really matter, take other's since its edges may not be |
| 664 |
// pixel aligned, so after intersection clip behavior should respect its aa type. |
| 665 |
fAA = other.fAA; |
| 666 |
} else if (!GrClip::IsPixelAligned(other.fShape.rect())) { |
| 667 |
// Neither shape is pixel aligned and AA types don't match so can't combine |
| 668 |
return false; |
| 669 |
} |
| 670 |
// Either we've updated this->fAA to actually match, or other->fAA doesn't matter so |
| 671 |
// this can be set to true. We just can't modify other to set it's aa to this->fAA. |
| 672 |
// But since 'this' becomes the combo of the two, other will be deleted so that's fine. |
| 673 |
aaMatch = true; |
| 674 |
} |
| 675 |
|
| 676 |
if (aaMatch && fLocalToDevice == other.fLocalToDevice) { |
| 677 |
if (!fShape.rect().intersect(other.fShape.rect())) { |
| 678 |
// By floating point, it turns out the combination should be empty |
| 679 |
this->fShape.reset(); |
| 680 |
this->markInvalid(current); |
| 681 |
return true; |
| 682 |
} |
| 683 |
shapeUpdated = true; |
| 684 |
} |
| 685 |
} else if ((fShape.isRect() || fShape.isRRect()) && |
| 686 |
(other.fShape.isRect() || other.fShape.isRRect())) { |
| 687 |
// No such pixel-aligned disregard for AA for round rects |
| 688 |
if (fAA == other.fAA && fLocalToDevice == other.fLocalToDevice) { |
| 689 |
// Treat rrect+rect intersections as rrect+rrect |
| 690 |
SkRRect a = fShape.isRect() ? SkRRect::MakeRect(fShape.rect()) : fShape.rrect(); |
| 691 |
SkRRect b = other.fShape.isRect() ? SkRRect::MakeRect(other.fShape.rect()) |
| 692 |
: other.fShape.rrect(); |
| 693 |
|
| 694 |
SkRRect joined = SkRRectPriv::ConservativeIntersect(a, b); |
| 695 |
if (!joined.isEmpty()) { |
| 696 |
// Can reduce to a single element |
| 697 |
if (joined.isRect()) { |
| 698 |
// And with a simplified type |
| 699 |
fShape.setRect(joined.rect()); |
| 700 |
} else { |
| 701 |
fShape.setRRect(joined); |
| 702 |
} |
| 703 |
shapeUpdated = true; |
| 704 |
} else if (!a.getBounds().intersects(b.getBounds())) { |
| 705 |
// Like the rect+rect combination, the intersection is actually empty |
| 706 |
fShape.reset(); |
| 707 |
this->markInvalid(current); |
| 708 |
return true; |
| 709 |
} |
| 710 |
} |
| 711 |
} |
| 712 |
|
| 713 |
if (shapeUpdated) { |
| 714 |
// This logic works under the assumption that both combined elements were intersect, so we |
| 715 |
// don't do the full bounds computations like in simplify(). |
| 716 |
SkASSERT(fOp == SkClipOp::kIntersect && other.fOp == SkClipOp::kIntersect); |
| 717 |
SkAssertResult(fOuterBounds.intersect(other.fOuterBounds)); |
| 718 |
if (!fInnerBounds.intersect(other.fInnerBounds)) { |
| 719 |
fInnerBounds = SkIRect::MakeEmpty(); |
| 720 |
} |
| 721 |
return true; |
| 722 |
} else { |
| 723 |
return false; |
| 724 |
} |
| 725 |
} |
| 726 |
|
| 727 |
void ClipStack::RawElement::updateForElement(RawElement* added, const SaveRecord& current) { |
| 728 |
if (this->isInvalid()) { |
| 729 |
// Already doesn't do anything, so skip this element |
| 730 |
return; |
| 731 |
} |
| 732 |
|
| 733 |
// 'A' refers to this element, 'B' refers to 'added'. |
| 734 |
switch (get_clip_geometry(*this, *added)) { |
| 735 |
case ClipGeometry::kEmpty: |
| 736 |
// Mark both elements as invalid to signal that the clip is fully empty |
| 737 |
this->markInvalid(current); |
| 738 |
added->markInvalid(current); |
| 739 |
break; |
| 740 |
|
| 741 |
case ClipGeometry::kAOnly: |
| 742 |
// This element already clips more than 'added', so mark 'added' is invalid to skip it |
| 743 |
added->markInvalid(current); |
| 744 |
break; |
| 745 |
|
| 746 |
case ClipGeometry::kBOnly: |
| 747 |
// 'added' clips more than this element, so mark this as invalid |
| 748 |
this->markInvalid(current); |
| 749 |
break; |
| 750 |
|
| 751 |
case ClipGeometry::kBoth: |
| 752 |
// Else the bounds checks think we need to keep both, but depending on the combination |
| 753 |
// of the ops and shape kinds, we may be able to do better. |
| 754 |
if (added->combine(*this, current)) { |
| 755 |
// 'added' now fully represents the combination of the two elements |
| 756 |
this->markInvalid(current); |
| 757 |
} |
| 758 |
break; |
| 759 |
} |
| 760 |
} |
| 761 |
|
| 762 |
ClipStack::ClipState ClipStack::RawElement::clipType() const { |
| 763 |
// Map from the internal shape kind to the clip state enum |
| 764 |
switch (fShape.type()) { |
| 765 |
case GrShape::Type::kEmpty: |
| 766 |
return ClipState::kEmpty; |
| 767 |
|
| 768 |
case GrShape::Type::kRect: |
| 769 |
return fOp == SkClipOp::kIntersect && fLocalToDevice.isIdentity() |
| 770 |
? ClipState::kDeviceRect : ClipState::kComplex; |
| 771 |
|
| 772 |
case GrShape::Type::kRRect: |
| 773 |
return fOp == SkClipOp::kIntersect && fLocalToDevice.isIdentity() |
| 774 |
? ClipState::kDeviceRRect : ClipState::kComplex; |
| 775 |
|
| 776 |
case GrShape::Type::kArc: |
| 777 |
case GrShape::Type::kLine: |
| 778 |
case GrShape::Type::kPoint: |
| 779 |
// These types should never become RawElements |
| 780 |
SkASSERT(false); |
| 781 |
[[fallthrough]]; |
| 782 |
|
| 783 |
case GrShape::Type::kPath: |
| 784 |
return ClipState::kComplex; |
| 785 |
} |
| 786 |
SkUNREACHABLE; |
| 787 |
} |
| 788 |
|
| 789 |
/////////////////////////////////////////////////////////////////////////////// |
| 790 |
// ClipStack::Mask |
| 791 |
|
| 792 |
ClipStack::Mask::Mask(const SaveRecord& current, const SkIRect& drawBounds) |
| 793 |
: fBounds(drawBounds) |
| 794 |
, fGenID(current.genID()) { |
| 795 |
static const UniqueKey::Domain kDomain = UniqueKey::GenerateDomain(); |
| 796 |
|
| 797 |
// The gen ID should not be invalid, empty, or wide open, since those do not require masks |
| 798 |
SkASSERT(fGenID != kInvalidGenID && fGenID != kEmptyGenID && fGenID != kWideOpenGenID); |
| 799 |
|
| 800 |
UniqueKey::Builder builder(&fKey, kDomain, 5, "clip_mask"); |
| 801 |
builder[0] = fGenID; |
| 802 |
builder[1] = drawBounds.fLeft; |
| 803 |
builder[2] = drawBounds.fRight; |
| 804 |
builder[3] = drawBounds.fTop; |
| 805 |
builder[4] = drawBounds.fBottom; |
| 806 |
SkASSERT(fKey.isValid()); |
| 807 |
|
| 808 |
SkDEBUGCODE(fOwner = ¤t;) |
| 809 |
} |
| 810 |
|
| 811 |
bool ClipStack::Mask::appliesToDraw(const SaveRecord& current, const SkIRect& drawBounds) const { |
| 812 |
// For the same save record, a larger mask will have the same or more elements |
| 813 |
// baked into it, so it can be reused to clip the smaller draw. |
| 814 |
SkASSERT(fGenID != current.genID() || ¤t == fOwner); |
| 815 |
return fGenID == current.genID() && fBounds.contains(drawBounds); |
| 816 |
} |
| 817 |
|
| 818 |
void ClipStack::Mask::invalidate(GrProxyProvider* proxyProvider) { |
| 819 |
SkASSERT(proxyProvider); |
| 820 |
SkASSERT(fKey.isValid()); // Should only be invalidated once |
| 821 |
proxyProvider->processInvalidUniqueKey( |
| 822 |
fKey, nullptr, GrProxyProvider::InvalidateGPUResource::kYes); |
| 823 |
fKey.reset(); |
| 824 |
} |
| 825 |
|
| 826 |
/////////////////////////////////////////////////////////////////////////////// |
| 827 |
// ClipStack::SaveRecord |
| 828 |
|
| 829 |
ClipStack::SaveRecord::SaveRecord(const SkIRect& deviceBounds) |
| 830 |
: fInnerBounds(deviceBounds) |
| 831 |
, fOuterBounds(deviceBounds) |
| 832 |
, fShader(nullptr) |
| 833 |
, fStartingMaskIndex(0) |
| 834 |
, fStartingElementIndex(0) |
| 835 |
, fOldestValidIndex(0) |
| 836 |
, fDeferredSaveCount(0) |
| 837 |
, fStackOp(SkClipOp::kIntersect) |
| 838 |
, fState(ClipState::kWideOpen) |
| 839 |
, fGenID(kInvalidGenID) {} |
| 840 |
|
| 841 |
ClipStack::SaveRecord::SaveRecord(const SaveRecord& prior, |
| 842 |
int startingMaskIndex, |
| 843 |
int startingElementIndex) |
| 844 |
: fInnerBounds(prior.fInnerBounds) |
| 845 |
, fOuterBounds(prior.fOuterBounds) |
| 846 |
, fShader(prior.fShader) |
| 847 |
, fStartingMaskIndex(startingMaskIndex) |
| 848 |
, fStartingElementIndex(startingElementIndex) |
| 849 |
, fOldestValidIndex(prior.fOldestValidIndex) |
| 850 |
, fDeferredSaveCount(0) |
| 851 |
, fStackOp(prior.fStackOp) |
| 852 |
, fState(prior.fState) |
| 853 |
, fGenID(kInvalidGenID) { |
| 854 |
// If the prior record never needed a mask, this one will insert into the same index |
| 855 |
// (that's okay since we'll remove it when this record is popped off the stack). |
| 856 |
SkASSERT(startingMaskIndex >= prior.fStartingMaskIndex); |
| 857 |
// The same goes for elements (the prior could have been wide open). |
| 858 |
SkASSERT(startingElementIndex >= prior.fStartingElementIndex); |
| 859 |
} |
| 860 |
|
| 861 |
uint32_t ClipStack::SaveRecord::genID() const { |
| 862 |
if (fState == ClipState::kEmpty) { |
| 863 |
return kEmptyGenID; |
| 864 |
} else if (fState == ClipState::kWideOpen) { |
| 865 |
return kWideOpenGenID; |
| 866 |
} else { |
| 867 |
// The gen ID shouldn't be empty or wide open, since they are reserved for the above |
| 868 |
// if-cases. It may be kInvalid if the record hasn't had any elements added to it yet. |
| 869 |
SkASSERT(fGenID != kEmptyGenID && fGenID != kWideOpenGenID); |
| 870 |
return fGenID; |
| 871 |
} |
| 872 |
} |
| 873 |
|
| 874 |
ClipStack::ClipState ClipStack::SaveRecord::state() const { |
| 875 |
if (fShader && fState != ClipState::kEmpty) { |
| 876 |
return ClipState::kComplex; |
| 877 |
} else { |
| 878 |
return fState; |
| 879 |
} |
| 880 |
} |
| 881 |
|
| 882 |
bool ClipStack::SaveRecord::contains(const ClipStack::Draw& draw) const { |
| 883 |
return fInnerBounds.contains(draw.outerBounds()); |
| 884 |
} |
| 885 |
|
| 886 |
bool ClipStack::SaveRecord::contains(const ClipStack::RawElement& element) const { |
| 887 |
return fInnerBounds.contains(element.outerBounds()); |
| 888 |
} |
| 889 |
|
| 890 |
void ClipStack::SaveRecord::removeElements(RawElement::Stack* elements) { |
| 891 |
while (elements->count() > fStartingElementIndex) { |
| 892 |
elements->pop_back(); |
| 893 |
} |
| 894 |
} |
| 895 |
|
| 896 |
void ClipStack::SaveRecord::restoreElements(RawElement::Stack* elements) { |
| 897 |
// Presumably this SaveRecord is the new top of the stack, and so it owns the elements |
| 898 |
// from its starting index to restoreCount - 1. Elements from the old save record have |
| 899 |
// been destroyed already, so their indices would have been >= restoreCount, and any |
| 900 |
// still-present element can be un-invalidated based on that. |
| 901 |
int i = elements->count() - 1; |
| 902 |
for (RawElement& e : elements->ritems()) { |
| 903 |
if (i < fOldestValidIndex) { |
| 904 |
break; |
| 905 |
} |
| 906 |
e.restoreValid(*this); |
| 907 |
--i; |
| 908 |
} |
| 909 |
} |
| 910 |
|
| 911 |
void ClipStack::SaveRecord::invalidateMasks(GrProxyProvider* proxyProvider, |
| 912 |
Mask::Stack* masks) { |
| 913 |
// Must explicitly invalidate the key before removing the mask object from the stack |
| 914 |
while (masks->count() > fStartingMaskIndex) { |
| 915 |
SkASSERT(masks->back().owner() == this && proxyProvider); |
| 916 |
masks->back().invalidate(proxyProvider); |
| 917 |
masks->pop_back(); |
| 918 |
} |
| 919 |
SkASSERT(masks->empty() || masks->back().genID() != fGenID); |
| 920 |
} |
| 921 |
|
| 922 |
void ClipStack::SaveRecord::reset(const SkIRect& bounds) { |
| 923 |
SkASSERT(this->canBeUpdated()); |
| 924 |
fOldestValidIndex = fStartingElementIndex; |
| 925 |
fOuterBounds = bounds; |
| 926 |
fInnerBounds = bounds; |
| 927 |
fStackOp = SkClipOp::kIntersect; |
| 928 |
fState = ClipState::kWideOpen; |
| 929 |
fShader = nullptr; |
| 930 |
} |
| 931 |
|
| 932 |
void ClipStack::SaveRecord::addShader(sk_sp<SkShader> shader) { |
| 933 |
SkASSERT(shader); |
| 934 |
SkASSERT(this->canBeUpdated()); |
| 935 |
if (!fShader) { |
| 936 |
fShader = std::move(shader); |
| 937 |
} else { |
| 938 |
// The total coverage is computed by multiplying the coverage from each element (shape or |
| 939 |
// shader), but since multiplication is associative, we can use kSrcIn blending to make |
| 940 |
// a new shader that represents 'shader' * 'fShader' |
| 941 |
fShader = SkShaders::Blend(SkBlendMode::kSrcIn, std::move(shader), fShader); |
| 942 |
} |
| 943 |
} |
| 944 |
|
| 945 |
bool ClipStack::SaveRecord::addElement(RawElement&& toAdd, RawElement::Stack* elements) { |
| 946 |
// Validity check the element's state first; if the shape class isn't empty, the outer bounds |
| 947 |
// shouldn't be empty; if the inner bounds are not empty, they must be contained in outer. |
| 948 |
SkASSERT((toAdd.shape().isEmpty() || !toAdd.outerBounds().isEmpty()) && |
| 949 |
(toAdd.innerBounds().isEmpty() || toAdd.outerBounds().contains(toAdd.innerBounds()))); |
| 950 |
// And we shouldn't be adding an element if we have a deferred save |
| 951 |
SkASSERT(this->canBeUpdated()); |
| 952 |
|
| 953 |
if (fState == ClipState::kEmpty) { |
| 954 |
// The clip is already empty, and we only shrink, so there's no need to record this element. |
| 955 |
return false; |
| 956 |
} else if (toAdd.shape().isEmpty()) { |
| 957 |
// An empty difference op should have been detected earlier, since it's a no-op |
| 958 |
SkASSERT(toAdd.op() == SkClipOp::kIntersect); |
| 959 |
fState = ClipState::kEmpty; |
| 960 |
return true; |
| 961 |
} |
| 962 |
|
| 963 |
// In this invocation, 'A' refers to the existing stack's bounds and 'B' refers to the new |
| 964 |
// element. |
| 965 |
switch (get_clip_geometry(*this, toAdd)) { |
| 966 |
case ClipGeometry::kEmpty: |
| 967 |
// The combination results in an empty clip |
| 968 |
fState = ClipState::kEmpty; |
| 969 |
return true; |
| 970 |
|
| 971 |
case ClipGeometry::kAOnly: |
| 972 |
// The combination would not be any different than the existing clip |
| 973 |
return false; |
| 974 |
|
| 975 |
case ClipGeometry::kBOnly: |
| 976 |
// The combination would invalidate the entire existing stack and can be replaced with |
| 977 |
// just the new element. |
| 978 |
this->replaceWithElement(std::move(toAdd), elements); |
| 979 |
return true; |
| 980 |
|
| 981 |
case ClipGeometry::kBoth: |
| 982 |
// The new element combines in a complex manner, so update the stack's bounds based on |
| 983 |
// the combination of its and the new element's ops (handled below) |
| 984 |
break; |
| 985 |
} |
| 986 |
|
| 987 |
if (fState == ClipState::kWideOpen) { |
| 988 |
// When the stack was wide open and the clip effect was kBoth, the "complex" manner is |
| 989 |
// simply to keep the element and update the stack bounds to be the element's intersected |
| 990 |
// with the device. |
| 991 |
this->replaceWithElement(std::move(toAdd), elements); |
| 992 |
return true; |
| 993 |
} |
| 994 |
|
| 995 |
// Some form of actual clip element(s) to combine with. |
| 996 |
if (fStackOp == SkClipOp::kIntersect) { |
| 997 |
if (toAdd.op() == SkClipOp::kIntersect) { |
| 998 |
// Intersect (stack) + Intersect (toAdd) |
| 999 |
// - Bounds updates is simply the paired intersections of outer and inner. |
| 1000 |
SkAssertResult(fOuterBounds.intersect(toAdd.outerBounds())); |
| 1001 |
if (!fInnerBounds.intersect(toAdd.innerBounds())) { |
| 1002 |
// NOTE: this does the right thing if either rect is empty, since we set the |
| 1003 |
// inner bounds to empty here |
| 1004 |
fInnerBounds = SkIRect::MakeEmpty(); |
| 1005 |
} |
| 1006 |
} else { |
| 1007 |
// Intersect (stack) + Difference (toAdd) |
| 1008 |
// - Shrink the stack's outer bounds if the difference op's inner bounds completely |
| 1009 |
// cuts off an edge. |
| 1010 |
// - Shrink the stack's inner bounds to completely exclude the op's outer bounds. |
| 1011 |
fOuterBounds = subtract(fOuterBounds, toAdd.innerBounds(), /* exact */ true); |
| 1012 |
fInnerBounds = subtract(fInnerBounds, toAdd.outerBounds(), /* exact */ false); |
| 1013 |
} |
| 1014 |
} else { |
| 1015 |
if (toAdd.op() == SkClipOp::kIntersect) { |
| 1016 |
// Difference (stack) + Intersect (toAdd) |
| 1017 |
// - Bounds updates are just the mirror of Intersect(stack) + Difference(toAdd) |
| 1018 |
SkIRect oldOuter = fOuterBounds; |
| 1019 |
fOuterBounds = subtract(toAdd.outerBounds(), fInnerBounds, /* exact */ true); |
| 1020 |
fInnerBounds = subtract(toAdd.innerBounds(), oldOuter, /* exact */ false); |
| 1021 |
} else { |
| 1022 |
// Difference (stack) + Difference (toAdd) |
| 1023 |
// - The updated outer bounds is the union of outer bounds and the inner becomes the |
| 1024 |
// largest of the two possible inner bounds |
| 1025 |
fOuterBounds.join(toAdd.outerBounds()); |
| 1026 |
if (toAdd.innerBounds().width() * toAdd.innerBounds().height() > |
| 1027 |
fInnerBounds.width() * fInnerBounds.height()) { |
| 1028 |
fInnerBounds = toAdd.innerBounds(); |
| 1029 |
} |
| 1030 |
} |
| 1031 |
} |
| 1032 |
|
| 1033 |
// If we get here, we're keeping the new element and the stack's bounds have been updated. |
| 1034 |
// We ought to have caught the cases where the stack bounds resemble an empty or wide open |
| 1035 |
// clip, so assert that's the case. |
| 1036 |
SkASSERT(!fOuterBounds.isEmpty() && |
| 1037 |
(fInnerBounds.isEmpty() || fOuterBounds.contains(fInnerBounds))); |
| 1038 |
|
| 1039 |
return this->appendElement(std::move(toAdd), elements); |
| 1040 |
} |
| 1041 |
|
| 1042 |
bool ClipStack::SaveRecord::appendElement(RawElement&& toAdd, RawElement::Stack* elements) { |
| 1043 |
// Update past elements to account for the new element |
| 1044 |
int i = elements->count() - 1; |
| 1045 |
|
| 1046 |
// After the loop, elements between [max(youngestValid, startingIndex)+1, count-1] can be |
| 1047 |
// removed from the stack (these are the active elements that have been invalidated by the |
| 1048 |
// newest element; since it's the active part of the stack, no restore() can bring them back). |
| 1049 |
int youngestValid = fStartingElementIndex - 1; |
| 1050 |
// After the loop, elements between [0, oldestValid-1] are all invalid. The value of oldestValid |
| 1051 |
// becomes the save record's new fLastValidIndex value. |
| 1052 |
int oldestValid = elements->count(); |
| 1053 |
// After the loop, this is the earliest active element that was invalidated. It may be |
| 1054 |
// older in the stack than earliestValid, so cannot be popped off, but can be used to store |
| 1055 |
// the new element instead of allocating more. |
| 1056 |
RawElement* oldestActiveInvalid = nullptr; |
| 1057 |
int oldestActiveInvalidIndex = elements->count(); |
| 1058 |
|
| 1059 |
for (RawElement& existing : elements->ritems()) { |
| 1060 |
if (i < fOldestValidIndex) { |
| 1061 |
break; |
| 1062 |
} |
| 1063 |
// We don't need to pass the actual index that toAdd will be saved to; just the minimum |
| 1064 |
// index of this save record, since that will result in the same restoration behavior later. |
| 1065 |
existing.updateForElement(&toAdd, *this); |
| 1066 |
|
| 1067 |
if (toAdd.isInvalid()) { |
| 1068 |
if (existing.isInvalid()) { |
| 1069 |
// Both new and old invalid implies the entire clip becomes empty |
| 1070 |
fState = ClipState::kEmpty; |
| 1071 |
return true; |
| 1072 |
} else { |
| 1073 |
// The new element doesn't change the clip beyond what the old element already does |
| 1074 |
return false; |
| 1075 |
} |
| 1076 |
} else if (existing.isInvalid()) { |
| 1077 |
// The new element cancels out the old element. The new element may have been modified |
| 1078 |
// to account for the old element's geometry. |
| 1079 |
if (i >= fStartingElementIndex) { |
| 1080 |
// Still active, so the invalidated index could be used to store the new element |
| 1081 |
oldestActiveInvalid = &existing; |
| 1082 |
oldestActiveInvalidIndex = i; |
| 1083 |
} |
| 1084 |
} else { |
| 1085 |
// Keep both new and old elements |
| 1086 |
oldestValid = i; |
| 1087 |
if (i > youngestValid) { |
| 1088 |
youngestValid = i; |
| 1089 |
} |
| 1090 |
} |
| 1091 |
|
| 1092 |
--i; |
| 1093 |
} |
| 1094 |
|
| 1095 |
// Post-iteration validity check |
| 1096 |
SkASSERT(oldestValid == elements->count() || |
| 1097 |
(oldestValid >= fOldestValidIndex && oldestValid < elements->count())); |
| 1098 |
SkASSERT(youngestValid == fStartingElementIndex - 1 || |
| 1099 |
(youngestValid >= fStartingElementIndex && youngestValid < elements->count())); |
| 1100 |
SkASSERT((oldestActiveInvalid && oldestActiveInvalidIndex >= fStartingElementIndex && |
| 1101 |
oldestActiveInvalidIndex < elements->count()) || !oldestActiveInvalid); |
| 1102 |
|
| 1103 |
// Update final state |
| 1104 |
SkASSERT(oldestValid >= fOldestValidIndex); |
| 1105 |
fOldestValidIndex = std::min(oldestValid, oldestActiveInvalidIndex); |
| 1106 |
fState = oldestValid == elements->count() ? toAdd.clipType() : ClipState::kComplex; |
| 1107 |
if (fStackOp == SkClipOp::kDifference && toAdd.op() == SkClipOp::kIntersect) { |
| 1108 |
// The stack remains in difference mode only as long as all elements are difference |
| 1109 |
fStackOp = SkClipOp::kIntersect; |
| 1110 |
} |
| 1111 |
|
| 1112 |
int targetCount = youngestValid + 1; |
| 1113 |
if (!oldestActiveInvalid || oldestActiveInvalidIndex >= targetCount) { |
| 1114 |
// toAdd will be stored right after youngestValid |
| 1115 |
targetCount++; |
| 1116 |
oldestActiveInvalid = nullptr; |
| 1117 |
} |
| 1118 |
while (elements->count() > targetCount) { |
| 1119 |
SkASSERT(oldestActiveInvalid != &elements->back()); // shouldn't delete what we'll reuse |
| 1120 |
elements->pop_back(); |
| 1121 |
} |
| 1122 |
if (oldestActiveInvalid) { |
| 1123 |
*oldestActiveInvalid = std::move(toAdd); |
| 1124 |
} else if (elements->count() < targetCount) { |
| 1125 |
elements->push_back(std::move(toAdd)); |
| 1126 |
} else { |
| 1127 |
elements->back() = std::move(toAdd); |
| 1128 |
} |
| 1129 |
|
| 1130 |
// Changing this will prompt ClipStack to invalidate any masks associated with this record. |
| 1131 |
fGenID = next_gen_id(); |
| 1132 |
return true; |
| 1133 |
} |
| 1134 |
|
| 1135 |
void ClipStack::SaveRecord::replaceWithElement(RawElement&& toAdd, RawElement::Stack* elements) { |
| 1136 |
// The aggregate state of the save record mirrors the element |
| 1137 |
fInnerBounds = toAdd.innerBounds(); |
| 1138 |
fOuterBounds = toAdd.outerBounds(); |
| 1139 |
fStackOp = toAdd.op(); |
| 1140 |
fState = toAdd.clipType(); |
| 1141 |
|
| 1142 |
// All prior active element can be removed from the stack: [startingIndex, count - 1] |
| 1143 |
int targetCount = fStartingElementIndex + 1; |
| 1144 |
while (elements->count() > targetCount) { |
| 1145 |
elements->pop_back(); |
| 1146 |
} |
| 1147 |
if (elements->count() < targetCount) { |
| 1148 |
elements->push_back(std::move(toAdd)); |
| 1149 |
} else { |
| 1150 |
elements->back() = std::move(toAdd); |
| 1151 |
} |
| 1152 |
|
| 1153 |
SkASSERT(elements->count() == fStartingElementIndex + 1); |
| 1154 |
|
| 1155 |
// This invalidates all older elements that are owned by save records lower in the clip stack. |
| 1156 |
fOldestValidIndex = fStartingElementIndex; |
| 1157 |
fGenID = next_gen_id(); |
| 1158 |
} |
| 1159 |
|
| 1160 |
/////////////////////////////////////////////////////////////////////////////// |
| 1161 |
// ClipStack |
| 1162 |
|
| 1163 |
// NOTE: Based on draw calls in all GMs, SKPs, and SVGs as of 08/20, 98% use a clip stack with |
| 1164 |
// one Element and up to two SaveRecords, thus the inline size for RawElement::Stack and |
| 1165 |
// SaveRecord::Stack (this conveniently keeps the size of ClipStack manageable). The max |
| 1166 |
// encountered element stack depth was 5 and the max save depth was 6. Using an increment of 8 for |
| 1167 |
// these stacks means that clip management will incur a single allocation for the remaining 2% |
| 1168 |
// of the draws, with extra head room for more complex clips encountered in the wild. |
| 1169 |
// |
| 1170 |
// The mask stack increment size was chosen to be smaller since only 0.2% of the evaluated draw call |
| 1171 |
// set ever used a mask (which includes stencil masks), or up to 0.3% when the atlas is disabled. |
| 1172 |
static constexpr int kElementStackIncrement = 8; |
| 1173 |
static constexpr int kSaveStackIncrement = 8; |
| 1174 |
static constexpr int kMaskStackIncrement = 4; |
| 1175 |
|
| 1176 |
// And from this same draw call set, the most complex clip could only use 5 analytic coverage FPs. |
| 1177 |
// Historically we limited it to 4 based on Blink's call pattern, so we keep the limit as-is since |
| 1178 |
// it's so close to the empirically encountered max. |
| 1179 |
static constexpr int kMaxAnalyticFPs = 4; |
| 1180 |
// The number of stack-allocated mask pointers to store before extending the arrays. |
| 1181 |
// Stack size determined empirically, the maximum number of elements put in a SW mask was 4 |
| 1182 |
// across our set of GMs, SKPs, and SVGs used for testing. |
| 1183 |
static constexpr int kNumStackMasks = 4; |
| 1184 |
|
| 1185 |
ClipStack::ClipStack(const SkIRect& deviceBounds, const SkMatrix* ctm, bool forceAA) |
| 1186 |
: fElements(kElementStackIncrement) |
| 1187 |
, fSaves(kSaveStackIncrement) |
| 1188 |
, fMasks(kMaskStackIncrement) |
| 1189 |
, fProxyProvider(nullptr) |
| 1190 |
, fDeviceBounds(deviceBounds) |
| 1191 |
, fCTM(ctm) |
| 1192 |
, fForceAA(forceAA) { |
| 1193 |
// Start with a save record that is wide open |
| 1194 |
fSaves.emplace_back(deviceBounds); |
| 1195 |
} |
| 1196 |
|
| 1197 |
ClipStack::~ClipStack() { |
| 1198 |
// Invalidate all mask keys that remain. Since we're tearing the clip stack down, we don't need |
| 1199 |
// to go through SaveRecord. |
| 1200 |
SkASSERT(fProxyProvider || fMasks.empty()); |
| 1201 |
if (fProxyProvider) { |
| 1202 |
for (Mask& m : fMasks.ritems()) { |
| 1203 |
m.invalidate(fProxyProvider); |
| 1204 |
} |
| 1205 |
} |
| 1206 |
} |
| 1207 |
|
| 1208 |
void ClipStack::save() { |
| 1209 |
SkASSERT(!fSaves.empty()); |
| 1210 |
fSaves.back().pushSave(); |
| 1211 |
} |
| 1212 |
|
| 1213 |
void ClipStack::restore() { |
| 1214 |
SkASSERT(!fSaves.empty()); |
| 1215 |
SaveRecord& current = fSaves.back(); |
| 1216 |
if (current.popSave()) { |
| 1217 |
// This was just a deferred save being undone, so the record doesn't need to be removed yet |
| 1218 |
return; |
| 1219 |
} |
| 1220 |
|
| 1221 |
// When we remove a save record, we delete all elements >= its starting index and any masks |
| 1222 |
// that were rasterized for it. |
| 1223 |
current.removeElements(&fElements); |
| 1224 |
SkASSERT(fProxyProvider || fMasks.empty()); |
| 1225 |
if (fProxyProvider) { |
| 1226 |
current.invalidateMasks(fProxyProvider, &fMasks); |
| 1227 |
} |
| 1228 |
fSaves.pop_back(); |
| 1229 |
// Restore any remaining elements that were only invalidated by the now-removed save record. |
| 1230 |
fSaves.back().restoreElements(&fElements); |
| 1231 |
} |
| 1232 |
|
| 1233 |
SkIRect ClipStack::getConservativeBounds() const { |
| 1234 |
const SaveRecord& current = this->currentSaveRecord(); |
| 1235 |
if (current.state() == ClipState::kEmpty) { |
| 1236 |
return SkIRect::MakeEmpty(); |
| 1237 |
} else if (current.state() == ClipState::kWideOpen) { |
| 1238 |
return fDeviceBounds; |
| 1239 |
} else { |
| 1240 |
if (current.op() == SkClipOp::kDifference) { |
| 1241 |
// The outer/inner bounds represent what's cut out, so full bounds remains the device |
| 1242 |
// bounds, minus any fully clipped content that spans the device edge. |
| 1243 |
return subtract(fDeviceBounds, current.innerBounds(), /* exact */ true); |
| 1244 |
} else { |
| 1245 |
SkASSERT(fDeviceBounds.contains(current.outerBounds())); |
| 1246 |
return current.outerBounds(); |
| 1247 |
} |
| 1248 |
} |
| 1249 |
} |
| 1250 |
|
| 1251 |
GrClip::PreClipResult ClipStack::preApply(const SkRect& bounds, GrAA aa) const { |
| 1252 |
Draw draw(bounds, fForceAA ? GrAA::kYes : aa); |
| 1253 |
if (!draw.applyDeviceBounds(fDeviceBounds)) { |
| 1254 |
return GrClip::Effect::kClippedOut; |
| 1255 |
} |
| 1256 |
|
| 1257 |
const SaveRecord& cs = this->currentSaveRecord(); |
| 1258 |
// Early out if we know a priori that the clip is full 0s or full 1s. |
| 1259 |
if (cs.state() == ClipState::kEmpty) { |
| 1260 |
return GrClip::Effect::kClippedOut; |
| 1261 |
} else if (cs.state() == ClipState::kWideOpen) { |
| 1262 |
SkASSERT(!cs.shader()); |
| 1263 |
return GrClip::Effect::kUnclipped; |
| 1264 |
} |
| 1265 |
|
| 1266 |
// Given argument order, 'A' == current clip, 'B' == draw |
| 1267 |
switch (get_clip_geometry(cs, draw)) { |
| 1268 |
case ClipGeometry::kEmpty: |
| 1269 |
// Can ignore the shader since the geometry removed everything already |
| 1270 |
return GrClip::Effect::kClippedOut; |
| 1271 |
|
| 1272 |
case ClipGeometry::kBOnly: |
| 1273 |
// Geometrically, the draw is unclipped, but can't ignore a shader |
| 1274 |
return cs.shader() ? GrClip::Effect::kClipped : GrClip::Effect::kUnclipped; |
| 1275 |
|
| 1276 |
case ClipGeometry::kAOnly: |
| 1277 |
// Shouldn't happen since the inner bounds of a draw are unknown |
| 1278 |
SkASSERT(false); |
| 1279 |
// But if it did, it technically means the draw covered the clip and should be |
| 1280 |
// considered kClipped or similar, which is what the next case handles. |
| 1281 |
[[fallthrough]]; |
| 1282 |
|
| 1283 |
case ClipGeometry::kBoth: { |
| 1284 |
SkASSERT(fElements.count() > 0); |
| 1285 |
const RawElement& back = fElements.back(); |
| 1286 |
if (cs.state() == ClipState::kDeviceRect) { |
| 1287 |
SkASSERT(back.clipType() == ClipState::kDeviceRect); |
| 1288 |
return {back.shape().rect(), back.aa()}; |
| 1289 |
} else if (cs.state() == ClipState::kDeviceRRect) { |
| 1290 |
SkASSERT(back.clipType() == ClipState::kDeviceRRect); |
| 1291 |
return {back.shape().rrect(), back.aa()}; |
| 1292 |
} else { |
| 1293 |
// The clip stack has complex shapes, multiple elements, or a shader; we could |
| 1294 |
// iterate per element like we would in apply(), but preApply() is meant to be |
| 1295 |
// conservative and efficient. |
| 1296 |
SkASSERT(cs.state() == ClipState::kComplex); |
| 1297 |
return GrClip::Effect::kClipped; |
| 1298 |
} |
| 1299 |
} |
| 1300 |
} |
| 1301 |
|
| 1302 |
SkUNREACHABLE; |
| 1303 |
} |
| 1304 |
|
| 1305 |
GrClip::Effect ClipStack::apply(GrRecordingContext* rContext, |
| 1306 |
SurfaceDrawContext* sdc, |
| 1307 |
GrDrawOp* op, |
| 1308 |
GrAAType aa, |
| 1309 |
GrAppliedClip* out, |
| 1310 |
SkRect* bounds) const { |
| 1311 |
// TODO: Once we no longer store SW masks, we don't need to sneak the provider in like this |
| 1312 |
if (!fProxyProvider) { |
| 1313 |
fProxyProvider = rContext->priv().proxyProvider(); |
| 1314 |
} |
| 1315 |
SkASSERT(fProxyProvider == rContext->priv().proxyProvider()); |
| 1316 |
const GrCaps* caps = rContext->priv().caps(); |
| 1317 |
|
| 1318 |
// Convert the bounds to a Draw and apply device bounds clipping, making our query as tight |
| 1319 |
// as possible. |
| 1320 |
Draw draw(*bounds, GrAA(fForceAA || aa != GrAAType::kNone)); |
| 1321 |
if (!draw.applyDeviceBounds(fDeviceBounds)) { |
| 1322 |
return Effect::kClippedOut; |
| 1323 |
} |
| 1324 |
SkAssertResult(bounds->intersect(SkRect::Make(fDeviceBounds))); |
| 1325 |
|
| 1326 |
const SaveRecord& cs = this->currentSaveRecord(); |
| 1327 |
// Early out if we know a priori that the clip is full 0s or full 1s. |
| 1328 |
if (cs.state() == ClipState::kEmpty) { |
| 1329 |
return Effect::kClippedOut; |
| 1330 |
} else if (cs.state() == ClipState::kWideOpen) { |
| 1331 |
SkASSERT(!cs.shader()); |
| 1332 |
return Effect::kUnclipped; |
| 1333 |
} |
| 1334 |
|
| 1335 |
// Convert any clip shader first, since it's not geometrically related to the draw bounds |
| 1336 |
std::unique_ptr<GrFragmentProcessor> clipFP = nullptr; |
| 1337 |
if (cs.shader()) { |
| 1338 |
static const GrColorInfo kCoverageColorInfo{GrColorType::kUnknown, kPremul_SkAlphaType, |
| 1339 |
nullptr}; |
| 1340 |
GrFPArgs args( |
| 1341 |
sdc, &kCoverageColorInfo, sdc->surfaceProps(), GrFPArgs::Scope::kDefault); |
| 1342 |
clipFP = GrFragmentProcessors::Make(cs.shader(), args, *fCTM); |
| 1343 |
if (clipFP) { |
| 1344 |
// The initial input is the coverage from the geometry processor, so this ensures it |
| 1345 |
// is multiplied properly with the alpha of the clip shader. |
| 1346 |
clipFP = GrFragmentProcessor::MulInputByChildAlpha(std::move(clipFP)); |
| 1347 |
} |
| 1348 |
} |
| 1349 |
|
| 1350 |
// A refers to the entire clip stack, B refers to the draw |
| 1351 |
switch (get_clip_geometry(cs, draw)) { |
| 1352 |
case ClipGeometry::kEmpty: |
| 1353 |
return Effect::kClippedOut; |
| 1354 |
|
| 1355 |
case ClipGeometry::kBOnly: |
| 1356 |
// Geometrically unclipped, but may need to add the shader as a coverage FP |
| 1357 |
if (clipFP) { |
| 1358 |
out->addCoverageFP(std::move(clipFP)); |
| 1359 |
return Effect::kClipped; |
| 1360 |
} else { |
| 1361 |
return Effect::kUnclipped; |
| 1362 |
} |
| 1363 |
|
| 1364 |
case ClipGeometry::kAOnly: |
| 1365 |
// Shouldn't happen since draws don't report inner bounds |
| 1366 |
SkASSERT(false); |
| 1367 |
[[fallthrough]]; |
| 1368 |
|
| 1369 |
case ClipGeometry::kBoth: |
| 1370 |
// The draw is combined with the saved clip elements; the below logic tries to skip |
| 1371 |
// as many elements as possible. |
| 1372 |
SkASSERT(cs.state() == ClipState::kDeviceRect || |
| 1373 |
cs.state() == ClipState::kDeviceRRect || |
| 1374 |
cs.state() == ClipState::kComplex); |
| 1375 |
break; |
| 1376 |
} |
| 1377 |
|
| 1378 |
// We can determine a scissor based on the draw and the overall stack bounds. |
| 1379 |
SkIRect scissorBounds; |
| 1380 |
if (cs.op() == SkClipOp::kIntersect) { |
| 1381 |
// Initially we keep this as large as possible; if the clip is applied solely with coverage |
| 1382 |
// FPs then using a loose scissor increases the chance we can batch the draws. |
| 1383 |
// We tighten it later if any form of mask or atlas element is needed. |
| 1384 |
scissorBounds = cs.outerBounds(); |
| 1385 |
} else { |
| 1386 |
scissorBounds = subtract(draw.outerBounds(), cs.innerBounds(), /* exact */ true); |
| 1387 |
} |
| 1388 |
|
| 1389 |
// We mark this true once we have a coverage FP (since complex clipping is occurring), or we |
| 1390 |
// have an element that wouldn't affect the scissored draw bounds, but does affect the regular |
| 1391 |
// draw bounds. In that case, the scissor is sufficient for clipping and we can skip the |
| 1392 |
// element but definitely cannot then drop the scissor. |
| 1393 |
bool scissorIsNeeded = SkToBool(cs.shader()); |
| 1394 |
SkDEBUGCODE(bool opClippedInternally = false;) |
| 1395 |
|
| 1396 |
int remainingAnalyticFPs = kMaxAnalyticFPs; |
| 1397 |
|
| 1398 |
// If window rectangles are supported, we can use them to exclude inner bounds of difference ops |
| 1399 |
int maxWindowRectangles = sdc->maxWindowRectangles(); |
| 1400 |
GrWindowRectangles windowRects; |
| 1401 |
|
| 1402 |
// Elements not represented as an analytic FP or skipped will be collected here and later |
| 1403 |
// applied by using the stencil buffer or a cached SW mask. |
| 1404 |
STArray<kNumStackMasks, const Element*> elementsForMask; |
| 1405 |
|
| 1406 |
bool maskRequiresAA = false; |
| 1407 |
auto atlasPathRenderer = rContext->priv().drawingManager()->getAtlasPathRenderer(); |
| 1408 |
|
| 1409 |
int i = fElements.count(); |
| 1410 |
for (const RawElement& e : fElements.ritems()) { |
| 1411 |
--i; |
| 1412 |
if (i < cs.oldestElementIndex()) { |
| 1413 |
// All earlier elements have been invalidated by elements already processed |
| 1414 |
break; |
| 1415 |
} else if (e.isInvalid()) { |
| 1416 |
continue; |
| 1417 |
} |
| 1418 |
|
| 1419 |
switch (get_clip_geometry(e, draw)) { |
| 1420 |
case ClipGeometry::kEmpty: |
| 1421 |
// This can happen for difference op elements that have a larger fInnerBounds than |
| 1422 |
// can be preserved at the next level. |
| 1423 |
return Effect::kClippedOut; |
| 1424 |
|
| 1425 |
case ClipGeometry::kBOnly: |
| 1426 |
// We don't need to produce a coverage FP or mask for the element |
| 1427 |
break; |
| 1428 |
|
| 1429 |
case ClipGeometry::kAOnly: |
| 1430 |
// Shouldn't happen for draws, fall through to regular element processing |
| 1431 |
SkASSERT(false); |
| 1432 |
[[fallthrough]]; |
| 1433 |
|
| 1434 |
case ClipGeometry::kBoth: { |
| 1435 |
// The element must apply coverage to the draw, enable the scissor to limit overdraw |
| 1436 |
scissorIsNeeded = true; |
| 1437 |
|
| 1438 |
// First apply using HW methods (scissor and window rects). When the inner and outer |
| 1439 |
// bounds match, nothing else needs to be done. |
| 1440 |
bool fullyApplied = false; |
| 1441 |
|
| 1442 |
// First check if the op knows how to apply this clip internally. |
| 1443 |
SkASSERT(!e.shape().inverted()); |
| 1444 |
auto result = op->clipToShape(sdc, e.op(), e.localToDevice(), e.shape(), |
| 1445 |
GrAA(e.aa() == GrAA::kYes || fForceAA)); |
| 1446 |
if (result != GrDrawOp::ClipResult::kFail) { |
| 1447 |
if (result == GrDrawOp::ClipResult::kClippedOut) { |
| 1448 |
return Effect::kClippedOut; |
| 1449 |
} |
| 1450 |
if (result == GrDrawOp::ClipResult::kClippedGeometrically) { |
| 1451 |
// The op clipped its own geometry. Tighten the draw bounds. |
| 1452 |
bounds->intersect(SkRect::Make(e.outerBounds())); |
| 1453 |
} |
| 1454 |
fullyApplied = true; |
| 1455 |
SkDEBUGCODE(opClippedInternally = true;) |
| 1456 |
} |
| 1457 |
|
| 1458 |
if (!fullyApplied) { |
| 1459 |
if (e.op() == SkClipOp::kIntersect) { |
| 1460 |
// The second test allows clipped draws that are scissored by multiple |
| 1461 |
// elements to remain scissor-only. |
| 1462 |
fullyApplied = e.innerBounds() == e.outerBounds() || |
| 1463 |
e.innerBounds().contains(scissorBounds); |
| 1464 |
} else { |
| 1465 |
if (!e.innerBounds().isEmpty() && |
| 1466 |
windowRects.count() < maxWindowRectangles) { |
| 1467 |
// TODO: If we have more difference ops than available window rects, we |
| 1468 |
// should prioritize those with the largest inner bounds. |
| 1469 |
windowRects.addWindow(e.innerBounds()); |
| 1470 |
fullyApplied = e.innerBounds() == e.outerBounds(); |
| 1471 |
} |
| 1472 |
} |
| 1473 |
} |
| 1474 |
|
| 1475 |
if (!fullyApplied && remainingAnalyticFPs > 0) { |
| 1476 |
std::tie(fullyApplied, clipFP) = analytic_clip_fp(e.asElement(), |
| 1477 |
*caps->shaderCaps(), |
| 1478 |
std::move(clipFP)); |
| 1479 |
if (!fullyApplied && atlasPathRenderer) { |
| 1480 |
std::tie(fullyApplied, clipFP) = clip_atlas_fp(sdc, op, |
| 1481 |
atlasPathRenderer, |
| 1482 |
scissorBounds, e.asElement(), |
| 1483 |
std::move(clipFP)); |
| 1484 |
} |
| 1485 |
if (fullyApplied) { |
| 1486 |
remainingAnalyticFPs--; |
| 1487 |
} |
| 1488 |
} |
| 1489 |
|
| 1490 |
if (!fullyApplied) { |
| 1491 |
elementsForMask.push_back(&e.asElement()); |
| 1492 |
maskRequiresAA |= (e.aa() == GrAA::kYes); |
| 1493 |
} |
| 1494 |
|
| 1495 |
break; |
| 1496 |
} |
| 1497 |
} |
| 1498 |
} |
| 1499 |
|
| 1500 |
if (!scissorIsNeeded) { |
| 1501 |
// More detailed analysis of the element shapes determined no clip is needed |
| 1502 |
SkASSERT(elementsForMask.empty() && !clipFP); |
| 1503 |
return Effect::kUnclipped; |
| 1504 |
} |
| 1505 |
|
| 1506 |
// Fill out the GrAppliedClip with what we know so far, possibly with a tightened scissor |
| 1507 |
if (cs.op() == SkClipOp::kIntersect && !elementsForMask.empty()) { |
| 1508 |
SkAssertResult(scissorBounds.intersect(draw.outerBounds())); |
| 1509 |
} |
| 1510 |
if (!GrClip::IsInsideClip(scissorBounds, *bounds, draw.aa())) { |
| 1511 |
out->hardClip().addScissor(scissorBounds, bounds); |
| 1512 |
} |
| 1513 |
if (!windowRects.empty()) { |
| 1514 |
out->hardClip().addWindowRectangles(windowRects, GrWindowRectsState::Mode::kExclusive); |
| 1515 |
} |
| 1516 |
|
| 1517 |
// Now rasterize any remaining elements, either to the stencil or a SW mask. All elements are |
| 1518 |
// flattened into a single mask. |
| 1519 |
if (!elementsForMask.empty()) { |
| 1520 |
bool stencilUnavailable = |
| 1521 |
!sdc->asRenderTargetProxy()->canUseStencil(*rContext->priv().caps()); |
| 1522 |
|
| 1523 |
bool hasSWMask = false; |
| 1524 |
if ((sdc->numSamples() <= 1 && !sdc->canUseDynamicMSAA() && maskRequiresAA) || |
| 1525 |
stencilUnavailable) { |
| 1526 |
// Must use a texture mask to represent the combined clip elements since the stencil |
| 1527 |
// cannot be used, or cannot handle smooth clips. |
| 1528 |
std::tie(hasSWMask, clipFP) = GetSWMaskFP( |
| 1529 |
rContext, &fMasks, cs, scissorBounds, elementsForMask.begin(), |
| 1530 |
elementsForMask.size(), std::move(clipFP)); |
| 1531 |
} |
| 1532 |
|
| 1533 |
if (!hasSWMask) { |
| 1534 |
if (stencilUnavailable) { |
| 1535 |
SkDebugf("WARNING: Clip mask requires stencil, but stencil unavailable. " |
| 1536 |
"Draw will be ignored.\n"); |
| 1537 |
return Effect::kClippedOut; |
| 1538 |
} else { |
| 1539 |
// Rasterize the remaining elements to the stencil buffer |
| 1540 |
render_stencil_mask(rContext, sdc, cs.genID(), scissorBounds, |
| 1541 |
elementsForMask.begin(), elementsForMask.size(), out); |
| 1542 |
} |
| 1543 |
} |
| 1544 |
} |
| 1545 |
|
| 1546 |
if (clipFP) { |
| 1547 |
// This will include all analytic FPs, all atlas FPs, and a SW mask FP. |
| 1548 |
out->addCoverageFP(std::move(clipFP)); |
| 1549 |
} |
| 1550 |
|
| 1551 |
SkASSERT(out->doesClip() || opClippedInternally); |
| 1552 |
return Effect::kClipped; |
| 1553 |
} |
| 1554 |
|
| 1555 |
ClipStack::SaveRecord& ClipStack::writableSaveRecord(bool* wasDeferred) { |
| 1556 |
SaveRecord& current = fSaves.back(); |
| 1557 |
if (current.canBeUpdated()) { |
| 1558 |
// Current record is still open, so it can be modified directly |
| 1559 |
*wasDeferred = false; |
| 1560 |
return current; |
| 1561 |
} else { |
| 1562 |
// Must undefer the save to get a new record. |
| 1563 |
SkAssertResult(current.popSave()); |
| 1564 |
*wasDeferred = true; |
| 1565 |
return fSaves.emplace_back(current, fMasks.count(), fElements.count()); |
| 1566 |
} |
| 1567 |
} |
| 1568 |
|
| 1569 |
void ClipStack::clipShader(sk_sp<SkShader> shader) { |
| 1570 |
// Shaders can't bring additional coverage |
| 1571 |
if (this->currentSaveRecord().state() == ClipState::kEmpty) { |
| 1572 |
return; |
| 1573 |
} |
| 1574 |
|
| 1575 |
bool wasDeferred; |
| 1576 |
this->writableSaveRecord(&wasDeferred).addShader(std::move(shader)); |
| 1577 |
// Masks and geometry elements are not invalidated by updating the clip shader |
| 1578 |
} |
| 1579 |
|
| 1580 |
void ClipStack::replaceClip(const SkIRect& rect) { |
| 1581 |
bool wasDeferred; |
| 1582 |
SaveRecord& save = this->writableSaveRecord(&wasDeferred); |
| 1583 |
|
| 1584 |
if (!wasDeferred) { |
| 1585 |
save.removeElements(&fElements); |
| 1586 |
save.invalidateMasks(fProxyProvider, &fMasks); |
| 1587 |
} |
| 1588 |
|
| 1589 |
save.reset(fDeviceBounds); |
| 1590 |
if (rect != fDeviceBounds) { |
| 1591 |
this->clipRect(SkMatrix::I(), SkRect::Make(rect), GrAA::kNo, SkClipOp::kIntersect); |
| 1592 |
} |
| 1593 |
} |
| 1594 |
|
| 1595 |
void ClipStack::clip(RawElement&& element) { |
| 1596 |
if (this->currentSaveRecord().state() == ClipState::kEmpty) { |
| 1597 |
return; |
| 1598 |
} |
| 1599 |
|
| 1600 |
// Reduce the path to anything simpler, will apply the transform if it's a scale+translate |
| 1601 |
// and ensures the element's bounds are clipped to the device (NOT the conservative clip bounds, |
| 1602 |
// since those are based on the net effect of all elements while device bounds clipping happens |
| 1603 |
// implicitly. During addElement, we may still be able to invalidate some older elements). |
| 1604 |
element.simplify(fDeviceBounds, fForceAA); |
| 1605 |
SkASSERT(!element.shape().inverted()); |
| 1606 |
|
| 1607 |
// An empty op means do nothing (for difference), or close the save record, so we try and detect |
| 1608 |
// that early before doing additional unnecessary save record allocation. |
| 1609 |
if (element.shape().isEmpty()) { |
| 1610 |
if (element.op() == SkClipOp::kDifference) { |
| 1611 |
// If the shape is empty and we're subtracting, this has no effect on the clip |
| 1612 |
return; |
| 1613 |
} |
| 1614 |
// else we will make the clip empty, but we need a new save record to record that change |
| 1615 |
// in the clip state; fall through to below and updateForElement() will handle it. |
| 1616 |
} |
| 1617 |
|
| 1618 |
bool wasDeferred; |
| 1619 |
SaveRecord& save = this->writableSaveRecord(&wasDeferred); |
| 1620 |
SkDEBUGCODE(uint32_t oldGenID = save.genID();) |
| 1621 |
SkDEBUGCODE(int elementCount = fElements.count();) |
| 1622 |
if (!save.addElement(std::move(element), &fElements)) { |
| 1623 |
if (wasDeferred) { |
| 1624 |
// We made a new save record, but ended up not adding an element to the stack. |
| 1625 |
// So instead of keeping an empty save record around, pop it off and restore the counter |
| 1626 |
SkASSERT(elementCount == fElements.count()); |
| 1627 |
fSaves.pop_back(); |
| 1628 |
fSaves.back().pushSave(); |
| 1629 |
} else { |
| 1630 |
// Should not have changed gen ID if the element and save were not modified |
| 1631 |
SkASSERT(oldGenID == save.genID()); |
| 1632 |
} |
| 1633 |
} else { |
| 1634 |
// The gen ID should be new, and should not be invalid |
| 1635 |
SkASSERT(oldGenID != save.genID() && save.genID() != kInvalidGenID); |
| 1636 |
if (fProxyProvider && !wasDeferred) { |
| 1637 |
// We modified an active save record so any old masks it had can be invalidated |
| 1638 |
save.invalidateMasks(fProxyProvider, &fMasks); |
| 1639 |
} |
| 1640 |
} |
| 1641 |
} |
| 1642 |
|
| 1643 |
GrFPResult ClipStack::GetSWMaskFP(GrRecordingContext* context, Mask::Stack* masks, |
| 1644 |
const SaveRecord& current, const SkIRect& bounds, |
| 1645 |
const Element** elements, int count, |
| 1646 |
std::unique_ptr<GrFragmentProcessor> clipFP) { |
| 1647 |
GrProxyProvider* proxyProvider = context->priv().proxyProvider(); |
| 1648 |
GrSurfaceProxyView maskProxy; |
| 1649 |
|
| 1650 |
SkIRect maskBounds; // may not be 'bounds' if we reuse a large clip mask |
| 1651 |
// Check the existing masks from this save record for compatibility |
| 1652 |
for (const Mask& m : masks->ritems()) { |
| 1653 |
if (m.genID() != current.genID()) { |
| 1654 |
break; |
| 1655 |
} |
| 1656 |
if (m.appliesToDraw(current, bounds)) { |
| 1657 |
maskProxy = proxyProvider->findCachedProxyWithColorTypeFallback( |
| 1658 |
m.key(), kMaskOrigin, GrColorType::kAlpha_8, 1); |
| 1659 |
if (maskProxy) { |
| 1660 |
maskBounds = m.bounds(); |
| 1661 |
break; |
| 1662 |
} |
| 1663 |
} |
| 1664 |
} |
| 1665 |
|
| 1666 |
if (!maskProxy) { |
| 1667 |
// No existing mask was found, so need to render a new one |
| 1668 |
maskProxy = render_sw_mask(context, bounds, elements, count); |
| 1669 |
if (!maskProxy) { |
| 1670 |
// If we still don't have one, there's nothing we can do |
| 1671 |
return GrFPFailure(std::move(clipFP)); |
| 1672 |
} |
| 1673 |
|
| 1674 |
// Register the mask for later invalidation |
| 1675 |
Mask& mask = masks->emplace_back(current, bounds); |
| 1676 |
proxyProvider->assignUniqueKeyToProxy(mask.key(), maskProxy.asTextureProxy()); |
| 1677 |
maskBounds = bounds; |
| 1678 |
} |
| 1679 |
|
| 1680 |
// Wrap the mask in an FP that samples it for coverage |
| 1681 |
SkASSERT(maskProxy && maskProxy.origin() == kMaskOrigin); |
| 1682 |
|
| 1683 |
GrSamplerState samplerState(GrSamplerState::WrapMode::kClampToBorder, |
| 1684 |
GrSamplerState::Filter::kNearest); |
| 1685 |
// Maps the device coords passed to the texture effect to the top-left corner of the mask, and |
| 1686 |
// make sure that the draw bounds are pre-mapped into the mask's space as well. |
| 1687 |
auto m = SkMatrix::Translate(-maskBounds.fLeft, -maskBounds.fTop); |
| 1688 |
auto subset = SkRect::Make(bounds); |
| 1689 |
subset.offset(-maskBounds.fLeft, -maskBounds.fTop); |
| 1690 |
// We scissor to bounds. The mask's texel centers are aligned to device space |
| 1691 |
// pixel centers. Hence this domain of texture coordinates. |
| 1692 |
auto domain = subset.makeInset(0.5, 0.5); |
| 1693 |
auto fp = GrTextureEffect::MakeSubset(std::move(maskProxy), kPremul_SkAlphaType, m, |
| 1694 |
samplerState, subset, domain, *context->priv().caps()); |
| 1695 |
fp = GrFragmentProcessor::DeviceSpace(std::move(fp)); |
| 1696 |
|
| 1697 |
// Must combine the coverage sampled from the texture effect with the previous coverage |
| 1698 |
fp = GrBlendFragmentProcessor::Make<SkBlendMode::kDstIn>(std::move(fp), std::move(clipFP)); |
| 1699 |
return GrFPSuccess(std::move(fp)); |
| 1700 |
} |
| 1701 |
|
| 1702 |
} // namespace skgpu::ganesh |