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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
|
/*
* Copyright 2010 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "GrDrawTarget.h"
#include "GrCaps.h"
#include "GrGpu.h"
#include "GrPath.h"
#include "GrPipeline.h"
#include "GrMemoryPool.h"
#include "GrRenderTarget.h"
#include "GrResourceProvider.h"
#include "GrRenderTargetPriv.h"
#include "GrSurfacePriv.h"
#include "GrTexture.h"
#include "GrVertexBuffer.h"
#include "batches/GrClearBatch.h"
#include "batches/GrCopySurfaceBatch.h"
#include "batches/GrDiscardBatch.h"
#include "batches/GrDrawBatch.h"
#include "batches/GrDrawPathBatch.h"
#include "batches/GrRectBatchFactory.h"
#include "batches/GrStencilPathBatch.h"
#include "SkStrokeRec.h"
////////////////////////////////////////////////////////////////////////////////
// Experimentally we have found that most batching occurs within the first 10 comparisons.
static const int kDefaultMaxBatchLookback = 10;
GrDrawTarget::GrDrawTarget(GrRenderTarget* rt, GrGpu* gpu, GrResourceProvider* resourceProvider,
const Options& options)
: fGpu(SkRef(gpu))
, fResourceProvider(resourceProvider)
, fFlushing(false)
, fFlags(0)
, fRenderTarget(rt) {
// TODO: Stop extracting the context (currently needed by GrClipMaskManager)
fContext = fGpu->getContext();
fClipMaskManager.reset(new GrClipMaskManager(this, options.fClipBatchToBounds));
fDrawBatchBounds = options.fDrawBatchBounds;
fMaxBatchLookback = (options.fMaxBatchLookback < 0) ? kDefaultMaxBatchLookback :
options.fMaxBatchLookback;
rt->setLastDrawTarget(this);
#ifdef SK_DEBUG
static int debugID = 0;
fDebugID = debugID++;
#endif
}
GrDrawTarget::~GrDrawTarget() {
if (fRenderTarget && this == fRenderTarget->getLastDrawTarget()) {
fRenderTarget->setLastDrawTarget(nullptr);
}
fGpu->unref();
}
////////////////////////////////////////////////////////////////////////////////
// Add a GrDrawTarget-based dependency
void GrDrawTarget::addDependency(GrDrawTarget* dependedOn) {
SkASSERT(!dependedOn->dependsOn(this)); // loops are bad
if (this->dependsOn(dependedOn)) {
return; // don't add duplicate dependencies
}
*fDependencies.push() = dependedOn;
}
// Convert from a GrSurface-based dependency to a GrDrawTarget one
void GrDrawTarget::addDependency(GrSurface* dependedOn) {
if (dependedOn->asRenderTarget() && dependedOn->asRenderTarget()->getLastDrawTarget()) {
// If it is still receiving dependencies, this DT shouldn't be closed
SkASSERT(!this->isClosed());
GrDrawTarget* dt = dependedOn->asRenderTarget()->getLastDrawTarget();
if (dt == this) {
// self-read - presumably for dst reads
} else {
this->addDependency(dt);
// Can't make it closed in the self-read case
dt->makeClosed();
}
}
}
#ifdef SK_DEBUG
void GrDrawTarget::dump() const {
SkDebugf("--------------------------------------------------------------\n");
SkDebugf("node: %d -> RT: %d\n", fDebugID, fRenderTarget ? fRenderTarget->getUniqueID() : -1);
SkDebugf("relies On (%d): ", fDependencies.count());
for (int i = 0; i < fDependencies.count(); ++i) {
SkDebugf("%d, ", fDependencies[i]->fDebugID);
}
SkDebugf("\n");
SkDebugf("batches (%d):\n", fBatches.count());
for (int i = 0; i < fBatches.count(); ++i) {
#if 0
SkDebugf("*******************************\n");
#endif
SkDebugf("%d: %s\n", i, fBatches[i]->name());
#if 0
SkString str = fBatches[i]->dumpInfo();
SkDebugf("%s\n", str.c_str());
#endif
}
}
#endif
bool GrDrawTarget::setupDstReadIfNecessary(const GrPipelineBuilder& pipelineBuilder,
const GrPipelineOptimizations& optimizations,
GrXferProcessor::DstTexture* dstTexture,
const SkRect& batchBounds) {
SkRect bounds = batchBounds;
bounds.outset(0.5f, 0.5f);
if (!pipelineBuilder.willXPNeedDstTexture(*this->caps(), optimizations)) {
return true;
}
GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
if (this->caps()->textureBarrierSupport()) {
if (GrTexture* rtTex = rt->asTexture()) {
// The render target is a texture, so we can read from it directly in the shader. The XP
// will be responsible to detect this situation and request a texture barrier.
dstTexture->setTexture(rtTex);
dstTexture->setOffset(0, 0);
return true;
}
}
SkIRect copyRect;
pipelineBuilder.clip().getConservativeBounds(rt->width(), rt->height(), ©Rect);
SkIRect drawIBounds;
bounds.roundOut(&drawIBounds);
if (!copyRect.intersect(drawIBounds)) {
#ifdef SK_DEBUG
GrCapsDebugf(this->caps(), "Missed an early reject. "
"Bailing on draw from setupDstReadIfNecessary.\n");
#endif
return false;
}
// MSAA consideration: When there is support for reading MSAA samples in the shader we could
// have per-sample dst values by making the copy multisampled.
GrSurfaceDesc desc;
if (!fGpu->initCopySurfaceDstDesc(rt, &desc)) {
desc.fOrigin = kDefault_GrSurfaceOrigin;
desc.fFlags = kRenderTarget_GrSurfaceFlag;
desc.fConfig = rt->config();
}
desc.fWidth = copyRect.width();
desc.fHeight = copyRect.height();
static const uint32_t kFlags = 0;
SkAutoTUnref<GrTexture> copy(fResourceProvider->createApproxTexture(desc, kFlags));
if (!copy) {
SkDebugf("Failed to create temporary copy of destination texture.\n");
return false;
}
SkIPoint dstPoint = {0, 0};
this->copySurface(copy, rt, copyRect, dstPoint);
dstTexture->setTexture(copy);
dstTexture->setOffset(copyRect.fLeft, copyRect.fTop);
return true;
}
void GrDrawTarget::prepareBatches(GrBatchFlushState* flushState) {
if (fFlushing) {
return;
}
fFlushing = true;
// Semi-usually the drawTargets are already closed at this point, but sometimes Ganesh
// needs to flush mid-draw. In that case, the SkGpuDevice's drawTargets won't be closed
// but need to be flushed anyway. Closing such drawTargets here will mean new
// drawTargets will be created to replace them if the SkGpuDevice(s) write to them again.
this->makeClosed();
// Loop over the batches that haven't yet generated their geometry
for (int i = 0; i < fBatches.count(); ++i) {
fBatches[i]->prepare(flushState);
}
}
void GrDrawTarget::drawBatches(GrBatchFlushState* flushState) {
// Draw all the generated geometry.
SkRandom random;
for (int i = 0; i < fBatches.count(); ++i) {
if (fDrawBatchBounds) {
const SkRect& bounds = fBatches[i]->bounds();
SkIRect ibounds;
bounds.roundOut(&ibounds);
// In multi-draw buffer all the batches use the same render target and we won't need to
// get the batchs bounds.
if (GrRenderTarget* rt = fBatches[i]->renderTarget()) {
fGpu->drawDebugWireRect(rt, ibounds, 0xFF000000 | random.nextU());
}
}
fBatches[i]->draw(flushState);
}
fFlushing = false;
}
void GrDrawTarget::reset() {
fBatches.reset();
}
void GrDrawTarget::drawBatch(const GrPipelineBuilder& pipelineBuilder, GrDrawBatch* batch) {
// Setup clip
GrPipelineBuilder::AutoRestoreStencil ars;
GrAppliedClip clip;
if (!fClipMaskManager->setupClipping(pipelineBuilder, &ars, &batch->bounds(), &clip)) {
return;
}
GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps;
if (clip.clipCoverageFragmentProcessor()) {
arfps.set(&pipelineBuilder);
arfps.addCoverageFragmentProcessor(clip.clipCoverageFragmentProcessor());
}
GrPipeline::CreateArgs args;
if (!this->installPipelineInDrawBatch(&pipelineBuilder, &clip.scissorState(), batch)) {
return;
}
#ifdef ENABLE_MDB
SkASSERT(fRenderTarget);
batch->pipeline()->addDependenciesTo(fRenderTarget);
#endif
this->recordBatch(batch);
}
static const GrStencilSettings& winding_path_stencil_settings() {
GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
kIncClamp_StencilOp,
kIncClamp_StencilOp,
kAlwaysIfInClip_StencilFunc,
0xFFFF, 0xFFFF, 0xFFFF);
return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
}
static const GrStencilSettings& even_odd_path_stencil_settings() {
GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
kInvert_StencilOp,
kInvert_StencilOp,
kAlwaysIfInClip_StencilFunc,
0xFFFF, 0xFFFF, 0xFFFF);
return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
}
void GrDrawTarget::getPathStencilSettingsForFilltype(GrPathRendering::FillType fill,
const GrStencilAttachment* sb,
GrStencilSettings* outStencilSettings) {
switch (fill) {
default:
SkFAIL("Unexpected path fill.");
case GrPathRendering::kWinding_FillType:
*outStencilSettings = winding_path_stencil_settings();
break;
case GrPathRendering::kEvenOdd_FillType:
*outStencilSettings = even_odd_path_stencil_settings();
break;
}
fClipMaskManager->adjustPathStencilParams(sb, outStencilSettings);
}
void GrDrawTarget::stencilPath(const GrPipelineBuilder& pipelineBuilder,
const SkMatrix& viewMatrix,
const GrPath* path,
GrPathRendering::FillType fill) {
// TODO: extract portions of checkDraw that are relevant to path stenciling.
SkASSERT(path);
SkASSERT(this->caps()->shaderCaps()->pathRenderingSupport());
// Setup clip
GrPipelineBuilder::AutoRestoreStencil ars;
GrAppliedClip clip;
if (!fClipMaskManager->setupClipping(pipelineBuilder, &ars, nullptr, &clip)) {
return;
}
GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps;
if (clip.clipCoverageFragmentProcessor()) {
arfps.set(&pipelineBuilder);
arfps.addCoverageFragmentProcessor(clip.clipCoverageFragmentProcessor());
}
// set stencil settings for path
GrStencilSettings stencilSettings;
GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
GrStencilAttachment* sb = fResourceProvider->attachStencilAttachment(rt);
this->getPathStencilSettingsForFilltype(fill, sb, &stencilSettings);
GrBatch* batch = GrStencilPathBatch::Create(viewMatrix,
pipelineBuilder.isHWAntialias(),
stencilSettings, clip.scissorState(),
pipelineBuilder.getRenderTarget(),
path);
this->recordBatch(batch);
batch->unref();
}
void GrDrawTarget::drawPathBatch(const GrPipelineBuilder& pipelineBuilder,
GrDrawPathBatchBase* batch) {
// This looks like drawBatch() but there is an added wrinkle that stencil settings get inserted
// after setting up clipping but before onDrawBatch(). TODO: Figure out a better model for
// handling stencil settings WRT interactions between pipeline(builder), clipmaskmanager, and
// batches.
SkASSERT(this->caps()->shaderCaps()->pathRenderingSupport());
GrPipelineBuilder::AutoRestoreStencil ars;
GrAppliedClip clip;
if (!fClipMaskManager->setupClipping(pipelineBuilder, &ars, &batch->bounds(), &clip)) {
return;
}
GrPipelineBuilder::AutoRestoreFragmentProcessorState arfps;
if (clip.clipCoverageFragmentProcessor()) {
arfps.set(&pipelineBuilder);
arfps.addCoverageFragmentProcessor(clip.clipCoverageFragmentProcessor());
}
// Ensure the render target has a stencil buffer and get the stencil settings.
GrStencilSettings stencilSettings;
GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
GrStencilAttachment* sb = fResourceProvider->attachStencilAttachment(rt);
this->getPathStencilSettingsForFilltype(batch->fillType(), sb, &stencilSettings);
batch->setStencilSettings(stencilSettings);
GrPipeline::CreateArgs args;
if (!this->installPipelineInDrawBatch(&pipelineBuilder, &clip.scissorState(), batch)) {
return;
}
this->recordBatch(batch);
}
void GrDrawTarget::drawNonAARect(const GrPipelineBuilder& pipelineBuilder,
GrColor color,
const SkMatrix& viewMatrix,
const SkRect& rect) {
SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateNonAAFill(color, viewMatrix, rect,
nullptr, nullptr));
this->drawBatch(pipelineBuilder, batch);
}
void GrDrawTarget::drawNonAARect(const GrPipelineBuilder& pipelineBuilder,
GrColor color,
const SkMatrix& viewMatrix,
const SkRect& rect,
const SkMatrix& localMatrix) {
SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateNonAAFill(color, viewMatrix, rect,
nullptr, &localMatrix));
this->drawBatch(pipelineBuilder, batch);
}
void GrDrawTarget::drawNonAARect(const GrPipelineBuilder& pipelineBuilder,
GrColor color,
const SkMatrix& viewMatrix,
const SkRect& rect,
const SkRect& localRect) {
SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateNonAAFill(color, viewMatrix, rect,
&localRect, nullptr));
this->drawBatch(pipelineBuilder, batch);
}
void GrDrawTarget::drawAARect(const GrPipelineBuilder& pipelineBuilder,
GrColor color,
const SkMatrix& viewMatrix,
const SkRect& rect,
const SkRect& devRect) {
SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateAAFill(color, viewMatrix, rect,
devRect));
this->drawBatch(pipelineBuilder, batch);
}
void GrDrawTarget::clear(const SkIRect* rect,
GrColor color,
bool canIgnoreRect,
GrRenderTarget* renderTarget) {
SkIRect rtRect = SkIRect::MakeWH(renderTarget->width(), renderTarget->height());
SkIRect clippedRect;
if (!rect ||
(canIgnoreRect && this->caps()->fullClearIsFree()) ||
rect->contains(rtRect)) {
rect = &rtRect;
} else {
clippedRect = *rect;
if (!clippedRect.intersect(rtRect)) {
return;
}
rect = &clippedRect;
}
if (this->caps()->useDrawInsteadOfClear()) {
// This works around a driver bug with clear by drawing a rect instead.
// The driver will ignore a clear if it is the only thing rendered to a
// target before the target is read.
if (rect == &rtRect) {
this->discard(renderTarget);
}
GrPipelineBuilder pipelineBuilder;
pipelineBuilder.setXPFactory(
GrPorterDuffXPFactory::Create(SkXfermode::kSrc_Mode))->unref();
pipelineBuilder.setRenderTarget(renderTarget);
this->drawNonAARect(pipelineBuilder, color, SkMatrix::I(), *rect);
} else {
GrBatch* batch = new GrClearBatch(*rect, color, renderTarget);
this->recordBatch(batch);
batch->unref();
}
}
void GrDrawTarget::discard(GrRenderTarget* renderTarget) {
if (this->caps()->discardRenderTargetSupport()) {
GrBatch* batch = new GrDiscardBatch(renderTarget);
this->recordBatch(batch);
batch->unref();
}
}
////////////////////////////////////////////////////////////////////////////////
void GrDrawTarget::copySurface(GrSurface* dst,
GrSurface* src,
const SkIRect& srcRect,
const SkIPoint& dstPoint) {
GrBatch* batch = GrCopySurfaceBatch::Create(dst, src, srcRect, dstPoint);
if (batch) {
#ifdef ENABLE_MDB
this->addDependency(src);
#endif
this->recordBatch(batch);
batch->unref();
}
}
template <class Left, class Right> static bool intersect(const Left& a, const Right& b) {
SkASSERT(a.fLeft <= a.fRight && a.fTop <= a.fBottom &&
b.fLeft <= b.fRight && b.fTop <= b.fBottom);
return a.fLeft < b.fRight && b.fLeft < a.fRight && a.fTop < b.fBottom && b.fTop < a.fBottom;
}
void GrDrawTarget::recordBatch(GrBatch* batch) {
// A closed drawTarget should never receive new/more batches
SkASSERT(!this->isClosed());
// Check if there is a Batch Draw we can batch with by linearly searching back until we either
// 1) check every draw
// 2) intersect with something
// 3) find a 'blocker'
GrBATCH_INFO("Re-Recording (%s, B%u)\n"
"\tBounds LRTB (%f, %f, %f, %f)\n",
batch->name(),
batch->uniqueID(),
batch->bounds().fLeft, batch->bounds().fRight,
batch->bounds().fTop, batch->bounds().fBottom);
GrBATCH_INFO(SkTabString(batch->dumpInfo(), 1).c_str());
GrBATCH_INFO("\tOutcome:\n");
int maxCandidates = SkTMin(fMaxBatchLookback, fBatches.count());
if (maxCandidates) {
int i = 0;
while (true) {
GrBatch* candidate = fBatches.fromBack(i);
// We cannot continue to search backwards if the render target changes
if (candidate->renderTargetUniqueID() != batch->renderTargetUniqueID()) {
GrBATCH_INFO("\t\tBreaking because of (%s, B%u) Rendertarget\n",
candidate->name(), candidate->uniqueID());
break;
}
if (candidate->combineIfPossible(batch, *this->caps())) {
GrBATCH_INFO("\t\tCombining with (%s, B%u)\n", candidate->name(),
candidate->uniqueID());
return;
}
// Stop going backwards if we would cause a painter's order violation.
// TODO: The bounds used here do not fully consider the clip. It may be advantageous
// to clip each batch's bounds to the clip.
if (intersect(candidate->bounds(), batch->bounds())) {
GrBATCH_INFO("\t\tIntersects with (%s, B%u)\n", candidate->name(),
candidate->uniqueID());
break;
}
++i;
if (i == maxCandidates) {
GrBATCH_INFO("\t\tReached max lookback or beginning of batch array %d\n", i);
break;
}
}
} else {
GrBATCH_INFO("\t\tFirstBatch\n");
}
fBatches.push_back().reset(SkRef(batch));
}
///////////////////////////////////////////////////////////////////////////////
bool GrDrawTarget::installPipelineInDrawBatch(const GrPipelineBuilder* pipelineBuilder,
const GrScissorState* scissor,
GrDrawBatch* batch) {
GrPipeline::CreateArgs args;
args.fPipelineBuilder = pipelineBuilder;
args.fCaps = this->caps();
args.fScissor = scissor;
batch->getPipelineOptimizations(&args.fOpts);
args.fOpts.fColorPOI.completeCalculations(pipelineBuilder->fColorFragmentProcessors.begin(),
pipelineBuilder->numColorFragmentProcessors());
args.fOpts.fCoveragePOI.completeCalculations(
pipelineBuilder->fCoverageFragmentProcessors.begin(),
pipelineBuilder->numCoverageFragmentProcessors());
if (!this->setupDstReadIfNecessary(*pipelineBuilder, args.fOpts, &args.fDstTexture,
batch->bounds())) {
return false;
}
if (!batch->installPipeline(args)) {
return false;
}
return true;
}
void GrDrawTarget::clearStencilClip(const SkIRect& rect, bool insideClip, GrRenderTarget* rt) {
GrBatch* batch = new GrClearStencilClipBatch(rect, insideClip, rt);
this->recordBatch(batch);
batch->unref();
}
|