1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
|
/*
* Copyright 2016 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "GrClipStackClip.h"
#include "GrAppliedClip.h"
#include "GrContextPriv.h"
#include "GrDrawingManager.h"
#include "GrRenderTargetContextPriv.h"
#include "GrFixedClip.h"
#include "GrGpuResourcePriv.h"
#include "GrRenderTargetPriv.h"
#include "GrStencilAttachment.h"
#include "GrSWMaskHelper.h"
#include "GrTextureProxy.h"
#include "effects/GrConvexPolyEffect.h"
#include "effects/GrRRectEffect.h"
#include "effects/GrTextureDomain.h"
#include "SkClipOpPriv.h"
typedef SkClipStack::Element Element;
typedef GrReducedClip::InitialState InitialState;
typedef GrReducedClip::ElementList ElementList;
static const int kMaxAnalyticElements = 4;
const char GrClipStackClip::kMaskTestTag[] = "clip_mask";
bool GrClipStackClip::quickContains(const SkRect& rect) const {
if (!fStack || fStack->isWideOpen()) {
return true;
}
return fStack->quickContains(rect);
}
bool GrClipStackClip::quickContains(const SkRRect& rrect) const {
if (!fStack || fStack->isWideOpen()) {
return true;
}
return fStack->quickContains(rrect);
}
bool GrClipStackClip::isRRect(const SkRect& origRTBounds, SkRRect* rr, GrAA* aa) const {
if (!fStack) {
return false;
}
const SkRect* rtBounds = &origRTBounds;
bool isAA;
if (fStack->isRRect(*rtBounds, rr, &isAA)) {
*aa = GrBoolToAA(isAA);
return true;
}
return false;
}
void GrClipStackClip::getConservativeBounds(int width, int height, SkIRect* devResult,
bool* isIntersectionOfRects) const {
if (!fStack) {
devResult->setXYWH(0, 0, width, height);
if (isIntersectionOfRects) {
*isIntersectionOfRects = true;
}
return;
}
SkRect devBounds;
fStack->getConservativeBounds(0, 0, width, height, &devBounds, isIntersectionOfRects);
devBounds.roundOut(devResult);
}
////////////////////////////////////////////////////////////////////////////////
// set up the draw state to enable the aa clipping mask.
static sk_sp<GrFragmentProcessor> create_fp_for_mask(GrResourceProvider* resourceProvider,
sk_sp<GrTextureProxy> mask,
const SkIRect &devBound) {
SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
return GrDeviceSpaceTextureDecalFragmentProcessor::Make(resourceProvider,
std::move(mask), domainTexels,
{devBound.fLeft, devBound.fTop});
}
// Does the path in 'element' require SW rendering? If so, return true (and,
// optionally, set 'prOut' to NULL. If not, return false (and, optionally, set
// 'prOut' to the non-SW path renderer that will do the job).
bool GrClipStackClip::PathNeedsSWRenderer(GrContext* context,
bool hasUserStencilSettings,
const GrRenderTargetContext* renderTargetContext,
const SkMatrix& viewMatrix,
const Element* element,
GrPathRenderer** prOut,
bool needsStencil) {
if (Element::kRect_Type == element->getType()) {
// rects can always be drawn directly w/o using the software path
// TODO: skip rrects once we're drawing them directly.
if (prOut) {
*prOut = nullptr;
}
return false;
} else {
// We shouldn't get here with an empty clip element.
SkASSERT(Element::kEmpty_Type != element->getType());
// the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
SkPath path;
element->asPath(&path);
if (path.isInverseFillType()) {
path.toggleInverseFillType();
}
GrPathRendererChain::DrawType type =
needsStencil ? GrPathRendererChain::DrawType::kStencilAndColor
: GrPathRendererChain::DrawType::kColor;
GrShape shape(path, GrStyle::SimpleFill());
GrPathRenderer::CanDrawPathArgs canDrawArgs;
canDrawArgs.fCaps = context->caps();
canDrawArgs.fViewMatrix = &viewMatrix;
canDrawArgs.fShape = &shape;
canDrawArgs.fAAType = GrChooseAAType(GrBoolToAA(element->isAA()),
renderTargetContext->fsaaType(),
GrAllowMixedSamples::kYes,
*context->caps());
canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
// the 'false' parameter disallows use of the SW path renderer
GrPathRenderer* pr =
context->contextPriv().drawingManager()->getPathRenderer(canDrawArgs, false, type);
if (prOut) {
*prOut = pr;
}
return SkToBool(!pr);
}
}
/*
* This method traverses the clip stack to see if the GrSoftwarePathRenderer
* will be used on any element. If so, it returns true to indicate that the
* entire clip should be rendered in SW and then uploaded en masse to the gpu.
*/
bool GrClipStackClip::UseSWOnlyPath(GrContext* context,
bool hasUserStencilSettings,
const GrRenderTargetContext* renderTargetContext,
const GrReducedClip& reducedClip) {
// TODO: generalize this function so that when
// a clip gets complex enough it can just be done in SW regardless
// of whether it would invoke the GrSoftwarePathRenderer.
// If we're avoiding stencils, always use SW:
if (context->caps()->avoidStencilBuffers())
return true;
// Set the matrix so that rendered clip elements are transformed to mask space from clip
// space.
SkMatrix translate;
translate.setTranslate(SkIntToScalar(-reducedClip.left()), SkIntToScalar(-reducedClip.top()));
for (ElementList::Iter iter(reducedClip.elements()); iter.get(); iter.next()) {
const Element* element = iter.get();
SkClipOp op = element->getOp();
bool invert = element->isInverseFilled();
bool needsStencil = invert ||
kIntersect_SkClipOp == op || kReverseDifference_SkClipOp == op;
if (PathNeedsSWRenderer(context, hasUserStencilSettings,
renderTargetContext, translate, element, nullptr, needsStencil)) {
return true;
}
}
return false;
}
static bool get_analytic_clip_processor(const ElementList& elements,
bool abortIfAA,
const SkRect& drawDevBounds,
sk_sp<GrFragmentProcessor>* resultFP) {
SkASSERT(elements.count() <= kMaxAnalyticElements);
SkSTArray<kMaxAnalyticElements, sk_sp<GrFragmentProcessor>> fps;
ElementList::Iter iter(elements);
while (iter.get()) {
SkClipOp op = iter.get()->getOp();
bool invert;
bool skip = false;
switch (op) {
case kReplace_SkClipOp:
SkASSERT(iter.get() == elements.head());
// Fallthrough, handled same as intersect.
case kIntersect_SkClipOp:
invert = false;
if (iter.get()->contains(drawDevBounds)) {
skip = true;
}
break;
case kDifference_SkClipOp:
invert = true;
// We don't currently have a cheap test for whether a rect is fully outside an
// element's primitive, so don't attempt to set skip.
break;
default:
return false;
}
if (!skip) {
GrPrimitiveEdgeType edgeType;
if (iter.get()->isAA()) {
if (abortIfAA) {
return false;
}
edgeType =
invert ? kInverseFillAA_GrProcessorEdgeType : kFillAA_GrProcessorEdgeType;
} else {
edgeType =
invert ? kInverseFillBW_GrProcessorEdgeType : kFillBW_GrProcessorEdgeType;
}
switch (iter.get()->getType()) {
case SkClipStack::Element::kPath_Type:
fps.emplace_back(GrConvexPolyEffect::Make(edgeType, iter.get()->getPath()));
break;
case SkClipStack::Element::kRRect_Type: {
fps.emplace_back(GrRRectEffect::Make(edgeType, iter.get()->getRRect()));
break;
}
case SkClipStack::Element::kRect_Type: {
fps.emplace_back(GrConvexPolyEffect::Make(edgeType, iter.get()->getRect()));
break;
}
default:
break;
}
if (!fps.back()) {
return false;
}
}
iter.next();
}
*resultFP = nullptr;
if (fps.count()) {
*resultFP = GrFragmentProcessor::RunInSeries(fps.begin(), fps.count());
}
return true;
}
////////////////////////////////////////////////////////////////////////////////
// sort out what kind of clip mask needs to be created: alpha, stencil,
// scissor, or entirely software
bool GrClipStackClip::apply(GrContext* context, GrRenderTargetContext* renderTargetContext,
bool useHWAA, bool hasUserStencilSettings, GrAppliedClip* out,
SkRect* bounds) const {
SkRect devBounds = SkRect::MakeIWH(renderTargetContext->width(), renderTargetContext->height());
if (!devBounds.intersect(*bounds)) {
return false;
}
if (!fStack || fStack->isWideOpen()) {
return true;
}
const GrReducedClip reducedClip(*fStack, devBounds,
renderTargetContext->priv().maxWindowRectangles());
if (reducedClip.hasIBounds() && !GrClip::IsInsideClip(reducedClip.ibounds(), devBounds)) {
out->addScissor(reducedClip.ibounds(), bounds);
}
if (!reducedClip.windowRectangles().empty()) {
out->addWindowRectangles(reducedClip.windowRectangles(),
GrWindowRectsState::Mode::kExclusive);
}
if (reducedClip.elements().isEmpty()) {
return InitialState::kAllIn == reducedClip.initialState();
}
#ifdef SK_DEBUG
SkASSERT(reducedClip.hasIBounds());
SkIRect rtIBounds = SkIRect::MakeWH(renderTargetContext->width(),
renderTargetContext->height());
const SkIRect& clipIBounds = reducedClip.ibounds();
SkASSERT(rtIBounds.contains(clipIBounds)); // Mask shouldn't be larger than the RT.
#endif
bool avoidStencilBuffers = context->caps()->avoidStencilBuffers();
// An element count of 4 was chosen because of the common pattern in Blink of:
// isect RR
// diff RR
// isect convex_poly
// isect convex_poly
// when drawing rounded div borders. This could probably be tuned based on a
// configuration's relative costs of switching RTs to generate a mask vs
// longer shaders.
if (reducedClip.elements().count() <= kMaxAnalyticElements) {
// When there are multiple samples we want to do per-sample clipping, not compute a
// fractional pixel coverage.
bool disallowAnalyticAA =
GrFSAAType::kNone != renderTargetContext->fsaaType() && !avoidStencilBuffers;
if (disallowAnalyticAA && !renderTargetContext->numColorSamples()) {
// With a single color sample, any coverage info is lost from color once it hits the
// color buffer anyway, so we may as well use coverage AA if nothing else in the pipe
// is multisampled.
disallowAnalyticAA = useHWAA || hasUserStencilSettings;
}
sk_sp<GrFragmentProcessor> clipFP;
if ((reducedClip.requiresAA() || avoidStencilBuffers) &&
get_analytic_clip_processor(reducedClip.elements(), disallowAnalyticAA, devBounds,
&clipFP)) {
out->addCoverageFP(std::move(clipFP));
return true;
}
}
// If the stencil buffer is multisampled we can use it to do everything.
if ((GrFSAAType::kNone == renderTargetContext->fsaaType() && reducedClip.requiresAA()) ||
avoidStencilBuffers) {
sk_sp<GrTextureProxy> result;
if (UseSWOnlyPath(context, hasUserStencilSettings, renderTargetContext, reducedClip)) {
// The clip geometry is complex enough that it will be more efficient to create it
// entirely in software
result = this->createSoftwareClipMask(context, reducedClip);
} else {
result = this->createAlphaClipMask(context, reducedClip);
}
if (result) {
// The mask's top left coord should be pinned to the rounded-out top left corner of
// the clip's device space bounds.
out->addCoverageFP(create_fp_for_mask(context->resourceProvider(), std::move(result),
reducedClip.ibounds()));
return true;
}
// If alpha or software clip mask creation fails, fall through to the stencil code paths,
// unless stencils are disallowed.
if (context->caps()->avoidStencilBuffers()) {
SkDebugf("WARNING: Clip mask requires stencil, but stencil unavailable. Clip will be ignored.\n");
return false;
}
}
GrRenderTarget* rt = renderTargetContext->accessRenderTarget();
if (!rt) {
return true;
}
// use the stencil clip if we can't represent the clip as a rectangle.
if (!context->resourceProvider()->attachStencilAttachment(rt)) {
SkDebugf("WARNING: failed to attach stencil buffer for clip mask. Clip will be ignored.\n");
return true;
}
// This relies on the property that a reduced sub-rect of the last clip will contain all the
// relevant window rectangles that were in the last clip. This subtle requirement will go away
// after clipping is overhauled.
if (renderTargetContext->priv().mustRenderClip(reducedClip.elementsGenID(),
reducedClip.ibounds())) {
reducedClip.drawStencilClipMask(context, renderTargetContext);
renderTargetContext->priv().setLastClip(reducedClip.elementsGenID(), reducedClip.ibounds());
}
out->addStencilClip();
return true;
}
////////////////////////////////////////////////////////////////////////////////
// Create a 8-bit clip mask in alpha
static void create_clip_mask_key(int32_t clipGenID, const SkIRect& bounds, GrUniqueKey* key) {
static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
GrUniqueKey::Builder builder(key, kDomain, 3, GrClipStackClip::kMaskTestTag);
builder[0] = clipGenID;
// SkToS16 because image filters outset layers to a size indicated by the filter, which can
// sometimes result in negative coordinates from device space.
builder[1] = SkToS16(bounds.fLeft) | (SkToS16(bounds.fRight) << 16);
builder[2] = SkToS16(bounds.fTop) | (SkToS16(bounds.fBottom) << 16);
}
static void add_invalidate_on_pop_message(const SkClipStack& stack, int32_t clipGenID,
const GrUniqueKey& clipMaskKey) {
SkClipStack::Iter iter(stack, SkClipStack::Iter::kTop_IterStart);
while (const Element* element = iter.prev()) {
if (element->getGenID() == clipGenID) {
std::unique_ptr<GrUniqueKeyInvalidatedMessage> msg(
new GrUniqueKeyInvalidatedMessage(clipMaskKey));
element->addResourceInvalidationMessage(std::move(msg));
return;
}
}
SkDEBUGFAIL("Gen ID was not found in stack.");
}
sk_sp<GrTextureProxy> GrClipStackClip::createAlphaClipMask(GrContext* context,
const GrReducedClip& reducedClip) const {
GrResourceProvider* resourceProvider = context->resourceProvider();
GrUniqueKey key;
create_clip_mask_key(reducedClip.elementsGenID(), reducedClip.ibounds(), &key);
sk_sp<GrTextureProxy> proxy(resourceProvider->findProxyByUniqueKey(key));
if (proxy) {
return proxy;
}
sk_sp<GrRenderTargetContext> rtc(context->makeDeferredRenderTargetContextWithFallback(
SkBackingFit::kApprox,
reducedClip.width(),
reducedClip.height(),
kAlpha_8_GrPixelConfig,
nullptr));
if (!rtc) {
return nullptr;
}
if (!reducedClip.drawAlphaClipMask(rtc.get())) {
return nullptr;
}
sk_sp<GrTextureProxy> result(rtc->asTextureProxyRef());
if (!result) {
return nullptr;
}
resourceProvider->assignUniqueKeyToProxy(key, result.get());
// MDB TODO (caching): this has to play nice with the GrSurfaceProxy's caching
add_invalidate_on_pop_message(*fStack, reducedClip.elementsGenID(), key);
return result;
}
sk_sp<GrTextureProxy> GrClipStackClip::createSoftwareClipMask(
GrContext* context,
const GrReducedClip& reducedClip) const {
GrUniqueKey key;
create_clip_mask_key(reducedClip.elementsGenID(), reducedClip.ibounds(), &key);
sk_sp<GrTextureProxy> proxy(context->resourceProvider()->findProxyByUniqueKey(key));
if (proxy) {
return proxy;
}
// The mask texture may be larger than necessary. We round out the clip bounds and pin the top
// left corner of the resulting rect to the top left of the texture.
SkIRect maskSpaceIBounds = SkIRect::MakeWH(reducedClip.width(), reducedClip.height());
GrSWMaskHelper helper;
// Set the matrix so that rendered clip elements are transformed to mask space from clip
// space.
SkMatrix translate;
translate.setTranslate(SkIntToScalar(-reducedClip.left()), SkIntToScalar(-reducedClip.top()));
if (!helper.init(maskSpaceIBounds, &translate)) {
return nullptr;
}
helper.clear(InitialState::kAllIn == reducedClip.initialState() ? 0xFF : 0x00);
for (ElementList::Iter iter(reducedClip.elements()); iter.get(); iter.next()) {
const Element* element = iter.get();
SkClipOp op = element->getOp();
GrAA aa = GrBoolToAA(element->isAA());
if (kIntersect_SkClipOp == op || kReverseDifference_SkClipOp == op) {
// Intersect and reverse difference require modifying pixels outside of the geometry
// that is being "drawn". In both cases we erase all the pixels outside of the geometry
// but leave the pixels inside the geometry alone. For reverse difference we invert all
// the pixels before clearing the ones outside the geometry.
if (kReverseDifference_SkClipOp == op) {
SkRect temp = SkRect::Make(reducedClip.ibounds());
// invert the entire scene
helper.drawRect(temp, SkRegion::kXOR_Op, GrAA::kNo, 0xFF);
}
SkPath clipPath;
element->asPath(&clipPath);
clipPath.toggleInverseFillType();
GrShape shape(clipPath, GrStyle::SimpleFill());
helper.drawShape(shape, SkRegion::kReplace_Op, aa, 0x00);
continue;
}
// The other ops (union, xor, diff) only affect pixels inside
// the geometry so they can just be drawn normally
if (Element::kRect_Type == element->getType()) {
helper.drawRect(element->getRect(), (SkRegion::Op)op, aa, 0xFF);
} else {
SkPath path;
element->asPath(&path);
GrShape shape(path, GrStyle::SimpleFill());
helper.drawShape(shape, (SkRegion::Op)op, aa, 0xFF);
}
}
sk_sp<GrTextureProxy> result(helper.toTextureProxy(context, SkBackingFit::kApprox));
context->resourceProvider()->assignUniqueKeyToProxy(key, result.get());
// MDB TODO (caching): this has to play nice with the GrSurfaceProxy's caching
add_invalidate_on_pop_message(*fStack, reducedClip.elementsGenID(), key);
return result;
}
|