1 /*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "GrRenderTargetContext.h"
9 #include "GrAppliedClip.h"
10 #include "GrColor.h"
11 #include "GrDrawingManager.h"
12 #include "GrFixedClip.h"
13 #include "GrGpuResourcePriv.h"
14 #include "GrPathRenderer.h"
15 #include "GrPipelineBuilder.h"
16 #include "GrRenderTarget.h"
17 #include "GrRenderTargetContextPriv.h"
18 #include "GrRenderTargetPriv.h"
19 #include "GrResourceProvider.h"
20 #include "GrStencilAttachment.h"
21 #include "SkLatticeIter.h"
22 #include "SkMatrixPriv.h"
23 #include "SkSurfacePriv.h"
24 #include "effects/GrRRectEffect.h"
25 #include "instanced/InstancedRendering.h"
26 #include "ops/GrClearOp.h"
27 #include "ops/GrDrawOp.h"
28 #include "ops/GrDrawAtlasOp.h"
29 #include "ops/GrDrawVerticesOp.h"
30 #include "ops/GrLatticeOp.h"
31 #include "ops/GrOp.h"
32 #include "ops/GrOvalOpFactory.h"
33 #include "ops/GrRectOpFactory.h"
34 #include "ops/GrRegionOp.h"
35 #include "ops/GrShadowRRectOp.h"
36 #include "ops/GrStencilPathOp.h"
37 #include "text/GrAtlasTextContext.h"
38 #include "text/GrStencilAndCoverTextContext.h"
39 #include "../private/GrAuditTrail.h"
40
41 #define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this->drawingManager()->getContext())
42 #define ASSERT_SINGLE_OWNER \
43 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fSingleOwner);)
44 #define ASSERT_SINGLE_OWNER_PRIV \
45 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fRenderTargetContext->fSingleOwner);)
46 #define RETURN_IF_ABANDONED if (this->drawingManager()->wasAbandoned()) { return; }
47 #define RETURN_IF_ABANDONED_PRIV if (fRenderTargetContext->drawingManager()->wasAbandoned()) { return; }
48 #define RETURN_FALSE_IF_ABANDONED if (this->drawingManager()->wasAbandoned()) { return false; }
49 #define RETURN_FALSE_IF_ABANDONED_PRIV if (fRenderTargetContext->drawingManager()->wasAbandoned()) { return false; }
50 #define RETURN_NULL_IF_ABANDONED if (this->drawingManager()->wasAbandoned()) { return nullptr; }
51
52 using gr_instanced::InstancedRendering;
53
54 class AutoCheckFlush {
55 public:
AutoCheckFlush(GrDrawingManager * drawingManager)56 AutoCheckFlush(GrDrawingManager* drawingManager) : fDrawingManager(drawingManager) {
57 SkASSERT(fDrawingManager);
58 }
~AutoCheckFlush()59 ~AutoCheckFlush() { fDrawingManager->flushIfNecessary(); }
60
61 private:
62 GrDrawingManager* fDrawingManager;
63 };
64
wasAbandoned() const65 bool GrRenderTargetContext::wasAbandoned() const {
66 return this->drawingManager()->wasAbandoned();
67 }
68
69 // In MDB mode the reffing of the 'getLastOpList' call's result allows in-progress
70 // GrOpLists to be picked up and added to by renderTargetContexts lower in the call
71 // stack. When this occurs with a closed GrOpList, a new one will be allocated
72 // when the renderTargetContext attempts to use it (via getOpList).
GrRenderTargetContext(GrContext * context,GrDrawingManager * drawingMgr,sk_sp<GrRenderTargetProxy> rtp,sk_sp<SkColorSpace> colorSpace,const SkSurfaceProps * surfaceProps,GrAuditTrail * auditTrail,GrSingleOwner * singleOwner)73 GrRenderTargetContext::GrRenderTargetContext(GrContext* context,
74 GrDrawingManager* drawingMgr,
75 sk_sp<GrRenderTargetProxy> rtp,
76 sk_sp<SkColorSpace> colorSpace,
77 const SkSurfaceProps* surfaceProps,
78 GrAuditTrail* auditTrail,
79 GrSingleOwner* singleOwner)
80 : GrSurfaceContext(context, drawingMgr, std::move(colorSpace), auditTrail, singleOwner)
81 , fRenderTargetProxy(std::move(rtp))
82 , fOpList(SkSafeRef(fRenderTargetProxy->getLastRenderTargetOpList()))
83 , fInstancedPipelineInfo(fRenderTargetProxy.get())
84 , fColorXformFromSRGB(nullptr)
85 , fSurfaceProps(SkSurfacePropsCopyOrDefault(surfaceProps)) {
86 if (fColorSpace) {
87 // sRGB sources are very common (SkColor, etc...), so we cache that gamut transformation
88 auto srgbColorSpace = SkColorSpace::MakeSRGB();
89 fColorXformFromSRGB = GrColorSpaceXform::Make(srgbColorSpace.get(), fColorSpace.get());
90 }
91 SkDEBUGCODE(this->validate();)
92 }
93
94 #ifdef SK_DEBUG
validate() const95 void GrRenderTargetContext::validate() const {
96 SkASSERT(fRenderTargetProxy);
97 fRenderTargetProxy->validate(fContext);
98
99 if (fOpList && !fOpList->isClosed()) {
100 SkASSERT(fRenderTargetProxy->getLastOpList() == fOpList);
101 }
102 }
103 #endif
104
~GrRenderTargetContext()105 GrRenderTargetContext::~GrRenderTargetContext() {
106 ASSERT_SINGLE_OWNER
107 SkSafeUnref(fOpList);
108 }
109
instantiate()110 GrRenderTarget* GrRenderTargetContext::instantiate() {
111 return fRenderTargetProxy->instantiate(fContext->resourceProvider());
112 }
113
asTextureProxy()114 GrTextureProxy* GrRenderTargetContext::asTextureProxy() {
115 return fRenderTargetProxy->asTextureProxy();
116 }
117
asTextureProxyRef()118 sk_sp<GrTextureProxy> GrRenderTargetContext::asTextureProxyRef() {
119 return sk_ref_sp(fRenderTargetProxy->asTextureProxy());
120 }
121
getOpList()122 GrRenderTargetOpList* GrRenderTargetContext::getOpList() {
123 ASSERT_SINGLE_OWNER
124 SkDEBUGCODE(this->validate();)
125
126 if (!fOpList || fOpList->isClosed()) {
127 fOpList = this->drawingManager()->newOpList(fRenderTargetProxy.get());
128 }
129
130 return fOpList;
131 }
132
133 // TODO: move this (and GrTextContext::copy) to GrSurfaceContext?
onCopy(GrSurfaceProxy * srcProxy,const SkIRect & srcRect,const SkIPoint & dstPoint)134 bool GrRenderTargetContext::onCopy(GrSurfaceProxy* srcProxy,
135 const SkIRect& srcRect,
136 const SkIPoint& dstPoint) {
137 ASSERT_SINGLE_OWNER
138 RETURN_FALSE_IF_ABANDONED
139 SkDEBUGCODE(this->validate();)
140 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::copy");
141
142 // TODO: defer instantiation until flush time
143 sk_sp<GrSurface> src(sk_ref_sp(srcProxy->instantiate(fContext->resourceProvider())));
144 if (!src) {
145 return false;
146 }
147
148 // TODO: This needs to be fixed up since it ends the deferral of the GrRenderTarget.
149 sk_sp<GrRenderTarget> rt(
150 sk_ref_sp(fRenderTargetProxy->instantiate(fContext->resourceProvider())));
151 if (!rt) {
152 return false;
153 }
154
155 return this->getOpList()->copySurface(rt.get(), src.get(), srcRect, dstPoint);
156 }
157
158 // TODO: move this (and GrTextureContext::onReadPixels) to GrSurfaceContext?
onReadPixels(const SkImageInfo & dstInfo,void * dstBuffer,size_t dstRowBytes,int x,int y,uint32_t flags)159 bool GrRenderTargetContext::onReadPixels(const SkImageInfo& dstInfo, void* dstBuffer,
160 size_t dstRowBytes, int x, int y, uint32_t flags) {
161 // TODO: teach GrRenderTarget to take ImageInfo directly to specify the src pixels
162 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo, *fContext->caps());
163 if (kUnknown_GrPixelConfig == config) {
164 return false;
165 }
166
167 // TODO: this seems to duplicate code in SkImage_Gpu::onReadPixels
168 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
169 flags |= GrContext::kUnpremul_PixelOpsFlag;
170 }
171
172 // Deferral of the VRAM resources must end in this instance anyway
173 sk_sp<GrRenderTarget> rt(
174 sk_ref_sp(fRenderTargetProxy->instantiate(fContext->resourceProvider())));
175 if (!rt) {
176 return false;
177 }
178
179 return rt->readPixels(this->getColorSpace(), x, y, dstInfo.width(), dstInfo.height(),
180 config, dstInfo.colorSpace(), dstBuffer, dstRowBytes, flags);
181 }
182
183 // TODO: move this (and GrTextureContext::onReadPixels) to GrSurfaceContext?
onWritePixels(const SkImageInfo & srcInfo,const void * srcBuffer,size_t srcRowBytes,int x,int y,uint32_t flags)184 bool GrRenderTargetContext::onWritePixels(const SkImageInfo& srcInfo, const void* srcBuffer,
185 size_t srcRowBytes, int x, int y, uint32_t flags) {
186 // TODO: teach GrRenderTarget to take ImageInfo directly to specify the src pixels
187 GrPixelConfig config = SkImageInfo2GrPixelConfig(srcInfo, *fContext->caps());
188 if (kUnknown_GrPixelConfig == config) {
189 return false;
190 }
191 if (kUnpremul_SkAlphaType == srcInfo.alphaType()) {
192 flags |= GrContext::kUnpremul_PixelOpsFlag;
193 }
194
195 // Deferral of the VRAM resources must end in this instance anyway
196 sk_sp<GrRenderTarget> rt(
197 sk_ref_sp(fRenderTargetProxy->instantiate(fContext->resourceProvider())));
198 if (!rt) {
199 return false;
200 }
201
202 return rt->writePixels(this->getColorSpace(), x, y, srcInfo.width(), srcInfo.height(),
203 config, srcInfo.colorSpace(), srcBuffer, srcRowBytes, flags);
204 }
205
206
drawText(const GrClip & clip,const SkPaint & skPaint,const SkMatrix & viewMatrix,const char text[],size_t byteLength,SkScalar x,SkScalar y,const SkIRect & clipBounds)207 void GrRenderTargetContext::drawText(const GrClip& clip, const SkPaint& skPaint,
208 const SkMatrix& viewMatrix, const char text[],
209 size_t byteLength, SkScalar x, SkScalar y,
210 const SkIRect& clipBounds) {
211 ASSERT_SINGLE_OWNER
212 RETURN_IF_ABANDONED
213 SkDEBUGCODE(this->validate();)
214 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawText");
215
216 GrAtlasTextContext* atlasTextContext = this->drawingManager()->getAtlasTextContext();
217 atlasTextContext->drawText(fContext, this, clip, skPaint, viewMatrix, fSurfaceProps, text,
218 byteLength, x, y, clipBounds);
219 }
220
drawPosText(const GrClip & clip,const SkPaint & paint,const SkMatrix & viewMatrix,const char text[],size_t byteLength,const SkScalar pos[],int scalarsPerPosition,const SkPoint & offset,const SkIRect & clipBounds)221 void GrRenderTargetContext::drawPosText(const GrClip& clip, const SkPaint& paint,
222 const SkMatrix& viewMatrix, const char text[],
223 size_t byteLength, const SkScalar pos[],
224 int scalarsPerPosition, const SkPoint& offset,
225 const SkIRect& clipBounds) {
226 ASSERT_SINGLE_OWNER
227 RETURN_IF_ABANDONED
228 SkDEBUGCODE(this->validate();)
229 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawPosText");
230
231 GrAtlasTextContext* atlasTextContext = this->drawingManager()->getAtlasTextContext();
232 atlasTextContext->drawPosText(fContext, this, clip, paint, viewMatrix, fSurfaceProps, text,
233 byteLength, pos, scalarsPerPosition, offset, clipBounds);
234 }
235
drawTextBlob(const GrClip & clip,const SkPaint & paint,const SkMatrix & viewMatrix,const SkTextBlob * blob,SkScalar x,SkScalar y,SkDrawFilter * filter,const SkIRect & clipBounds)236 void GrRenderTargetContext::drawTextBlob(const GrClip& clip, const SkPaint& paint,
237 const SkMatrix& viewMatrix, const SkTextBlob* blob,
238 SkScalar x, SkScalar y, SkDrawFilter* filter,
239 const SkIRect& clipBounds) {
240 ASSERT_SINGLE_OWNER
241 RETURN_IF_ABANDONED
242 SkDEBUGCODE(this->validate();)
243 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawTextBlob");
244
245 GrAtlasTextContext* atlasTextContext = this->drawingManager()->getAtlasTextContext();
246 atlasTextContext->drawTextBlob(fContext, this, clip, paint, viewMatrix, fSurfaceProps, blob, x,
247 y, filter, clipBounds);
248 }
249
discard()250 void GrRenderTargetContext::discard() {
251 ASSERT_SINGLE_OWNER
252 RETURN_IF_ABANDONED
253 SkDEBUGCODE(this->validate();)
254 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::discard");
255
256 AutoCheckFlush acf(this->drawingManager());
257
258 // TODO: This needs to be fixed up since it ends the deferral of the GrRenderTarget.
259 sk_sp<GrRenderTarget> rt(
260 sk_ref_sp(fRenderTargetProxy->instantiate(fContext->resourceProvider())));
261 if (!rt) {
262 return;
263 }
264
265 this->getOpList()->discard(this);
266 }
267
clear(const SkIRect * rect,const GrColor color,bool canIgnoreRect)268 void GrRenderTargetContext::clear(const SkIRect* rect,
269 const GrColor color,
270 bool canIgnoreRect) {
271 ASSERT_SINGLE_OWNER
272 RETURN_IF_ABANDONED
273 SkDEBUGCODE(this->validate();)
274 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::clear");
275
276 AutoCheckFlush acf(this->drawingManager());
277 this->internalClear(rect ? GrFixedClip(*rect) : GrFixedClip::Disabled(), color, canIgnoreRect);
278 }
279
absClear(const SkIRect * clearRect,const GrColor color)280 void GrRenderTargetContextPriv::absClear(const SkIRect* clearRect, const GrColor color) {
281 ASSERT_SINGLE_OWNER_PRIV
282 RETURN_IF_ABANDONED_PRIV
283 SkDEBUGCODE(fRenderTargetContext->validate();)
284 GR_AUDIT_TRAIL_AUTO_FRAME(fRenderTargetContext->fAuditTrail,
285 "GrRenderTargetContext::absClear");
286
287 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
288
289 SkIRect rtRect = SkIRect::MakeWH(fRenderTargetContext->fRenderTargetProxy->worstCaseWidth(
290 *fRenderTargetContext->caps()),
291 fRenderTargetContext->fRenderTargetProxy->worstCaseHeight(
292 *fRenderTargetContext->caps()));
293
294 if (clearRect) {
295 if (clearRect->contains(rtRect)) {
296 clearRect = nullptr; // full screen
297 } else {
298 if (!rtRect.intersect(*clearRect)) {
299 return;
300 }
301 }
302 }
303
304 // TODO: in a post-MDB world this should be handled at the OpList level.
305 // An op-list that is initially cleared and has no other ops should receive an
306 // extra draw.
307 if (fRenderTargetContext->fContext->caps()->useDrawInsteadOfClear()) {
308 // This works around a driver bug with clear by drawing a rect instead.
309 // The driver will ignore a clear if it is the only thing rendered to a
310 // target before the target is read.
311 GrPaint paint;
312 paint.setColor4f(GrColor4f::FromGrColor(color));
313 paint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
314
315 // We don't call drawRect() here to avoid the cropping to the, possibly smaller,
316 // RenderTargetProxy bounds
317 fRenderTargetContext->drawNonAAFilledRect(GrNoClip(), std::move(paint), SkMatrix::I(),
318 SkRect::Make(rtRect), nullptr, nullptr, nullptr,
319 GrAAType::kNone);
320
321 } else {
322 if (!fRenderTargetContext->accessRenderTarget()) {
323 return;
324 }
325
326 // This path doesn't handle coalescing of full screen clears b.c. it
327 // has to clear the entire render target - not just the content area.
328 // It could be done but will take more finagling.
329 std::unique_ptr<GrOp> op(GrClearOp::Make(
330 rtRect, color, fRenderTargetContext->accessRenderTarget(), !clearRect));
331 if (!op) {
332 return;
333 }
334 fRenderTargetContext->getOpList()->addOp(std::move(op), fRenderTargetContext);
335 }
336 }
337
clear(const GrFixedClip & clip,const GrColor color,bool canIgnoreClip)338 void GrRenderTargetContextPriv::clear(const GrFixedClip& clip,
339 const GrColor color,
340 bool canIgnoreClip) {
341 ASSERT_SINGLE_OWNER_PRIV
342 RETURN_IF_ABANDONED_PRIV
343 SkDEBUGCODE(fRenderTargetContext->validate();)
344 GR_AUDIT_TRAIL_AUTO_FRAME(fRenderTargetContext->fAuditTrail,
345 "GrRenderTargetContextPriv::clear");
346
347 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
348 fRenderTargetContext->internalClear(clip, color, canIgnoreClip);
349 }
350
internalClear(const GrFixedClip & clip,const GrColor color,bool canIgnoreClip)351 void GrRenderTargetContext::internalClear(const GrFixedClip& clip,
352 const GrColor color,
353 bool canIgnoreClip) {
354 bool isFull = false;
355 if (!clip.hasWindowRectangles()) {
356 isFull = !clip.scissorEnabled() ||
357 (canIgnoreClip && fContext->caps()->fullClearIsFree()) ||
358 clip.scissorRect().contains(SkIRect::MakeWH(this->width(), this->height()));
359 }
360
361 if (fContext->caps()->useDrawInsteadOfClear()) {
362 // This works around a driver bug with clear by drawing a rect instead.
363 // The driver will ignore a clear if it is the only thing rendered to a
364 // target before the target is read.
365 SkIRect clearRect = SkIRect::MakeWH(this->width(), this->height());
366 if (isFull) {
367 this->discard();
368 } else if (!clearRect.intersect(clip.scissorRect())) {
369 return;
370 }
371
372 GrPaint paint;
373 paint.setColor4f(GrColor4f::FromGrColor(color));
374 paint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
375
376 this->drawRect(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), SkRect::Make(clearRect));
377 } else if (isFull) {
378 if (this->accessRenderTarget()) {
379 this->getOpList()->fullClear(this, color);
380 }
381 } else {
382 if (!this->accessRenderTarget()) {
383 return;
384 }
385 std::unique_ptr<GrOp> op(GrClearOp::Make(clip, color, this->accessRenderTarget()));
386 if (!op) {
387 return;
388 }
389 this->getOpList()->addOp(std::move(op), this);
390 }
391 }
392
drawPaint(const GrClip & clip,GrPaint && paint,const SkMatrix & viewMatrix)393 void GrRenderTargetContext::drawPaint(const GrClip& clip,
394 GrPaint&& paint,
395 const SkMatrix& viewMatrix) {
396 ASSERT_SINGLE_OWNER
397 RETURN_IF_ABANDONED
398 SkDEBUGCODE(this->validate();)
399 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawPaint");
400
401 // set rect to be big enough to fill the space, but not super-huge, so we
402 // don't overflow fixed-point implementations
403
404 SkRect r = fRenderTargetProxy->getBoundsRect();
405
406 SkRRect rrect;
407 GrAA aa;
408 // Check if we can replace a clipRRect()/drawPaint() with a drawRRect(). We only do the
409 // transformation for non-rect rrects. Rects caused a performance regression on an Android
410 // test that needs investigation. We also skip cases where there are fragment processors
411 // because they may depend on having correct local coords and this path draws in device space
412 // without a local matrix.
413 if (!paint.numTotalFragmentProcessors() && clip.isRRect(r, &rrect, &aa) && !rrect.isRect()) {
414 this->drawRRect(GrNoClip(), std::move(paint), aa, SkMatrix::I(), rrect,
415 GrStyle::SimpleFill());
416 return;
417 }
418
419
420 bool isPerspective = viewMatrix.hasPerspective();
421
422 // We attempt to map r by the inverse matrix and draw that. mapRect will
423 // map the four corners and bound them with a new rect. This will not
424 // produce a correct result for some perspective matrices.
425 if (!isPerspective) {
426 if (!SkMatrixPriv::InverseMapRect(viewMatrix, &r, r)) {
427 SkDebugf("Could not invert matrix\n");
428 return;
429 }
430 this->drawRect(clip, std::move(paint), GrAA::kNo, viewMatrix, r);
431 } else {
432 SkMatrix localMatrix;
433 if (!viewMatrix.invert(&localMatrix)) {
434 SkDebugf("Could not invert matrix\n");
435 return;
436 }
437
438 AutoCheckFlush acf(this->drawingManager());
439
440 this->drawNonAAFilledRect(clip, std::move(paint), SkMatrix::I(), r, nullptr, &localMatrix,
441 nullptr, GrAAType::kNone);
442 }
443 }
444
rect_contains_inclusive(const SkRect & rect,const SkPoint & point)445 static inline bool rect_contains_inclusive(const SkRect& rect, const SkPoint& point) {
446 return point.fX >= rect.fLeft && point.fX <= rect.fRight &&
447 point.fY >= rect.fTop && point.fY <= rect.fBottom;
448 }
449
view_matrix_ok_for_aa_fill_rect(const SkMatrix & viewMatrix)450 static bool view_matrix_ok_for_aa_fill_rect(const SkMatrix& viewMatrix) {
451 return viewMatrix.preservesRightAngles();
452 }
453
454 // Attempts to crop a rect and optional local rect to the clip boundaries.
455 // Returns false if the draw can be skipped entirely.
crop_filled_rect(int width,int height,const GrClip & clip,const SkMatrix & viewMatrix,SkRect * rect,SkRect * localRect=nullptr)456 static bool crop_filled_rect(int width, int height, const GrClip& clip,
457 const SkMatrix& viewMatrix, SkRect* rect,
458 SkRect* localRect = nullptr) {
459 if (!viewMatrix.rectStaysRect()) {
460 return true;
461 }
462
463 SkIRect clipDevBounds;
464 SkRect clipBounds;
465
466 clip.getConservativeBounds(width, height, &clipDevBounds);
467 if (!SkMatrixPriv::InverseMapRect(viewMatrix, &clipBounds, SkRect::Make(clipDevBounds))) {
468 return false;
469 }
470
471 if (localRect) {
472 if (!rect->intersects(clipBounds)) {
473 return false;
474 }
475 const SkScalar dx = localRect->width() / rect->width();
476 const SkScalar dy = localRect->height() / rect->height();
477 if (clipBounds.fLeft > rect->fLeft) {
478 localRect->fLeft += (clipBounds.fLeft - rect->fLeft) * dx;
479 rect->fLeft = clipBounds.fLeft;
480 }
481 if (clipBounds.fTop > rect->fTop) {
482 localRect->fTop += (clipBounds.fTop - rect->fTop) * dy;
483 rect->fTop = clipBounds.fTop;
484 }
485 if (clipBounds.fRight < rect->fRight) {
486 localRect->fRight -= (rect->fRight - clipBounds.fRight) * dx;
487 rect->fRight = clipBounds.fRight;
488 }
489 if (clipBounds.fBottom < rect->fBottom) {
490 localRect->fBottom -= (rect->fBottom - clipBounds.fBottom) * dy;
491 rect->fBottom = clipBounds.fBottom;
492 }
493 return true;
494 }
495
496 return rect->intersect(clipBounds);
497 }
498
drawFilledRect(const GrClip & clip,GrPaint && paint,GrAA aa,const SkMatrix & viewMatrix,const SkRect & rect,const GrUserStencilSettings * ss)499 bool GrRenderTargetContext::drawFilledRect(const GrClip& clip,
500 GrPaint&& paint,
501 GrAA aa,
502 const SkMatrix& viewMatrix,
503 const SkRect& rect,
504 const GrUserStencilSettings* ss) {
505 SkRect croppedRect = rect;
506 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix, &croppedRect)) {
507 return true;
508 }
509
510 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport() &&
511 (!ss || ss->isDisabled(false))) {
512 InstancedRendering* ir = this->getOpList()->instancedRendering();
513 std::unique_ptr<GrDrawOp> op = ir->recordRect(croppedRect, viewMatrix, std::move(paint), aa,
514 fInstancedPipelineInfo);
515 if (op) {
516 this->addDrawOp(clip, std::move(op));
517 return true;
518 }
519 }
520 GrAAType aaType = this->decideAAType(aa);
521 if (GrAAType::kCoverage == aaType) {
522 // The fill path can handle rotation but not skew.
523 if (view_matrix_ok_for_aa_fill_rect(viewMatrix)) {
524 SkRect devBoundRect;
525 viewMatrix.mapRect(&devBoundRect, croppedRect);
526 std::unique_ptr<GrMeshDrawOp> op =
527 GrRectOpFactory::MakeAAFill(paint, viewMatrix, rect, croppedRect, devBoundRect);
528 if (op) {
529 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
530 if (ss) {
531 pipelineBuilder.setUserStencil(ss);
532 }
533 this->addMeshDrawOp(pipelineBuilder, clip, std::move(op));
534 return true;
535 }
536 }
537 } else {
538 this->drawNonAAFilledRect(clip, std::move(paint), viewMatrix, croppedRect, nullptr, nullptr,
539 ss, aaType);
540 return true;
541 }
542
543 return false;
544 }
545
drawRect(const GrClip & clip,GrPaint && paint,GrAA aa,const SkMatrix & viewMatrix,const SkRect & rect,const GrStyle * style)546 void GrRenderTargetContext::drawRect(const GrClip& clip,
547 GrPaint&& paint,
548 GrAA aa,
549 const SkMatrix& viewMatrix,
550 const SkRect& rect,
551 const GrStyle* style) {
552 if (!style) {
553 style = &GrStyle::SimpleFill();
554 }
555 ASSERT_SINGLE_OWNER
556 RETURN_IF_ABANDONED
557 SkDEBUGCODE(this->validate();)
558 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawRect");
559
560 // Path effects should've been devolved to a path in SkGpuDevice
561 SkASSERT(!style->pathEffect());
562
563 AutoCheckFlush acf(this->drawingManager());
564
565 const SkStrokeRec& stroke = style->strokeRec();
566 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
567
568 if (!fContext->caps()->useDrawInsteadOfClear()) {
569 // Check if this is a full RT draw and can be replaced with a clear. We don't bother
570 // checking cases where the RT is fully inside a stroke.
571 SkRect rtRect = fRenderTargetProxy->getBoundsRect();
572 // Does the clip contain the entire RT?
573 if (clip.quickContains(rtRect)) {
574 SkMatrix invM;
575 if (!viewMatrix.invert(&invM)) {
576 return;
577 }
578 // Does the rect bound the RT?
579 SkPoint srcSpaceRTQuad[4];
580 invM.mapRectToQuad(srcSpaceRTQuad, rtRect);
581 if (rect_contains_inclusive(rect, srcSpaceRTQuad[0]) &&
582 rect_contains_inclusive(rect, srcSpaceRTQuad[1]) &&
583 rect_contains_inclusive(rect, srcSpaceRTQuad[2]) &&
584 rect_contains_inclusive(rect, srcSpaceRTQuad[3])) {
585 // Will it blend?
586 GrColor clearColor;
587 if (paint.isConstantBlendedColor(&clearColor)) {
588 this->clear(nullptr, clearColor, true);
589 return;
590 }
591 }
592 }
593 }
594
595 if (this->drawFilledRect(clip, std::move(paint), aa, viewMatrix, rect, nullptr)) {
596 return;
597 }
598 } else if (stroke.getStyle() == SkStrokeRec::kStroke_Style ||
599 stroke.getStyle() == SkStrokeRec::kHairline_Style) {
600 if ((!rect.width() || !rect.height()) &&
601 SkStrokeRec::kHairline_Style != stroke.getStyle()) {
602 SkScalar r = stroke.getWidth() / 2;
603 // TODO: Move these stroke->fill fallbacks to GrShape?
604 switch (stroke.getJoin()) {
605 case SkPaint::kMiter_Join:
606 this->drawRect(
607 clip, std::move(paint), aa, viewMatrix,
608 {rect.fLeft - r, rect.fTop - r, rect.fRight + r, rect.fBottom + r},
609 &GrStyle::SimpleFill());
610 return;
611 case SkPaint::kRound_Join:
612 // Raster draws nothing when both dimensions are empty.
613 if (rect.width() || rect.height()){
614 SkRRect rrect = SkRRect::MakeRectXY(rect.makeOutset(r, r), r, r);
615 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect,
616 GrStyle::SimpleFill());
617 return;
618 }
619 case SkPaint::kBevel_Join:
620 if (!rect.width()) {
621 this->drawRect(clip, std::move(paint), aa, viewMatrix,
622 {rect.fLeft - r, rect.fTop, rect.fRight + r, rect.fBottom},
623 &GrStyle::SimpleFill());
624 } else {
625 this->drawRect(clip, std::move(paint), aa, viewMatrix,
626 {rect.fLeft, rect.fTop - r, rect.fRight, rect.fBottom + r},
627 &GrStyle::SimpleFill());
628 }
629 return;
630 }
631 }
632
633 bool snapToPixelCenters = false;
634 std::unique_ptr<GrMeshDrawOp> op;
635
636 GrColor color = paint.getColor();
637 GrAAType aaType = this->decideAAType(aa);
638 if (GrAAType::kCoverage == aaType) {
639 // The stroke path needs the rect to remain axis aligned (no rotation or skew).
640 if (viewMatrix.rectStaysRect()) {
641 op = GrRectOpFactory::MakeAAStroke(color, viewMatrix, rect, stroke);
642 }
643 } else {
644 // Depending on sub-pixel coordinates and the particular GPU, we may lose a corner of
645 // hairline rects. We jam all the vertices to pixel centers to avoid this, but not
646 // when MSAA is enabled because it can cause ugly artifacts.
647 snapToPixelCenters = stroke.getStyle() == SkStrokeRec::kHairline_Style &&
648 !fRenderTargetProxy->isUnifiedMultisampled();
649 op = GrRectOpFactory::MakeNonAAStroke(color, viewMatrix, rect, stroke,
650 snapToPixelCenters);
651 }
652
653 if (op) {
654 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
655 pipelineBuilder.setSnapVerticesToPixelCenters(snapToPixelCenters);
656 this->addMeshDrawOp(pipelineBuilder, clip, std::move(op));
657 return;
658 }
659 }
660
661 SkPath path;
662 path.setIsVolatile(true);
663 path.addRect(rect);
664 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, *style);
665 }
666
maxWindowRectangles() const667 int GrRenderTargetContextPriv::maxWindowRectangles() const {
668 return fRenderTargetContext->fRenderTargetProxy->maxWindowRectangles(
669 *fRenderTargetContext->fContext->caps());
670 }
671
clearStencilClip(const GrFixedClip & clip,bool insideStencilMask)672 void GrRenderTargetContextPriv::clearStencilClip(const GrFixedClip& clip, bool insideStencilMask) {
673 ASSERT_SINGLE_OWNER_PRIV
674 RETURN_IF_ABANDONED_PRIV
675 SkDEBUGCODE(fRenderTargetContext->validate();)
676 GR_AUDIT_TRAIL_AUTO_FRAME(fRenderTargetContext->fAuditTrail,
677 "GrRenderTargetContextPriv::clearStencilClip");
678
679 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
680 // TODO: This needs to be fixed up since it ends the deferral of the GrRenderTarget.
681 if (!fRenderTargetContext->accessRenderTarget()) {
682 return;
683 }
684 fRenderTargetContext->getOpList()->clearStencilClip(clip, insideStencilMask,
685 fRenderTargetContext);
686 }
687
stencilPath(const GrClip & clip,GrAAType aaType,const SkMatrix & viewMatrix,const GrPath * path)688 void GrRenderTargetContextPriv::stencilPath(const GrClip& clip,
689 GrAAType aaType,
690 const SkMatrix& viewMatrix,
691 const GrPath* path) {
692 ASSERT_SINGLE_OWNER_PRIV
693 RETURN_IF_ABANDONED_PRIV
694 SkDEBUGCODE(fRenderTargetContext->validate();)
695 GR_AUDIT_TRAIL_AUTO_FRAME(fRenderTargetContext->fAuditTrail,
696 "GrRenderTargetContext::stencilPath");
697
698 SkASSERT(aaType != GrAAType::kCoverage);
699
700 bool useHWAA = GrAATypeIsHW(aaType);
701 // TODO: extract portions of checkDraw that are relevant to path stenciling.
702 SkASSERT(path);
703 SkASSERT(fRenderTargetContext->caps()->shaderCaps()->pathRenderingSupport());
704
705 // FIXME: Use path bounds instead of this WAR once
706 // https://bugs.chromium.org/p/skia/issues/detail?id=5640 is resolved.
707 SkRect bounds = SkRect::MakeIWH(fRenderTargetContext->width(), fRenderTargetContext->height());
708
709 // Setup clip
710 GrAppliedClip appliedClip;
711 if (!clip.apply(fRenderTargetContext->fContext, fRenderTargetContext, useHWAA, true,
712 &appliedClip, &bounds)) {
713 return;
714 }
715
716 // Coverage AA does not make sense when rendering to the stencil buffer. The caller should never
717 // attempt this in a situation that would require coverage AA.
718 SkASSERT(!appliedClip.clipCoverageFragmentProcessor());
719
720 GrRenderTarget* rt = fRenderTargetContext->accessRenderTarget();
721 if (!rt) {
722 return;
723 }
724 GrStencilAttachment* stencilAttachment =
725 fRenderTargetContext->fContext->resourceProvider()->attachStencilAttachment(rt);
726 if (!stencilAttachment) {
727 SkDebugf("ERROR creating stencil attachment. Draw skipped.\n");
728 return;
729 }
730
731 std::unique_ptr<GrOp> op = GrStencilPathOp::Make(viewMatrix,
732 useHWAA,
733 path->getFillType(),
734 appliedClip.hasStencilClip(),
735 stencilAttachment->bits(),
736 appliedClip.scissorState(),
737 fRenderTargetContext->accessRenderTarget(),
738 path);
739 op->setClippedBounds(bounds);
740 fRenderTargetContext->getOpList()->recordOp(std::move(op), fRenderTargetContext);
741 }
742
stencilRect(const GrClip & clip,const GrUserStencilSettings * ss,GrAAType aaType,const SkMatrix & viewMatrix,const SkRect & rect)743 void GrRenderTargetContextPriv::stencilRect(const GrClip& clip,
744 const GrUserStencilSettings* ss,
745 GrAAType aaType,
746 const SkMatrix& viewMatrix,
747 const SkRect& rect) {
748 ASSERT_SINGLE_OWNER_PRIV
749 RETURN_IF_ABANDONED_PRIV
750 SkDEBUGCODE(fRenderTargetContext->validate();)
751 GR_AUDIT_TRAIL_AUTO_FRAME(fRenderTargetContext->fAuditTrail,
752 "GrRenderTargetContext::stencilRect");
753 SkASSERT(GrAAType::kCoverage != aaType);
754 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
755
756 GrPaint paint;
757 paint.setXPFactory(GrDisableColorXPFactory::Get());
758
759 fRenderTargetContext->drawNonAAFilledRect(clip, std::move(paint), viewMatrix, rect, nullptr,
760 nullptr, ss, aaType);
761 }
762
drawAndStencilRect(const GrClip & clip,const GrUserStencilSettings * ss,SkRegion::Op op,bool invert,GrAA aa,const SkMatrix & viewMatrix,const SkRect & rect)763 bool GrRenderTargetContextPriv::drawAndStencilRect(const GrClip& clip,
764 const GrUserStencilSettings* ss,
765 SkRegion::Op op,
766 bool invert,
767 GrAA aa,
768 const SkMatrix& viewMatrix,
769 const SkRect& rect) {
770 ASSERT_SINGLE_OWNER_PRIV
771 RETURN_FALSE_IF_ABANDONED_PRIV
772 SkDEBUGCODE(fRenderTargetContext->validate();)
773 GR_AUDIT_TRAIL_AUTO_FRAME(fRenderTargetContext->fAuditTrail,
774 "GrRenderTargetContext::drawAndStencilRect");
775
776 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
777
778 GrPaint paint;
779 paint.setCoverageSetOpXPFactory(op, invert);
780
781 if (fRenderTargetContext->drawFilledRect(clip, std::move(paint), aa, viewMatrix, rect, ss)) {
782 return true;
783 }
784 SkPath path;
785 path.setIsVolatile(true);
786 path.addRect(rect);
787 return this->drawAndStencilPath(clip, ss, op, invert, aa, viewMatrix, path);
788 }
789
fillRectToRect(const GrClip & clip,GrPaint && paint,GrAA aa,const SkMatrix & viewMatrix,const SkRect & rectToDraw,const SkRect & localRect)790 void GrRenderTargetContext::fillRectToRect(const GrClip& clip,
791 GrPaint&& paint,
792 GrAA aa,
793 const SkMatrix& viewMatrix,
794 const SkRect& rectToDraw,
795 const SkRect& localRect) {
796 ASSERT_SINGLE_OWNER
797 RETURN_IF_ABANDONED
798 SkDEBUGCODE(this->validate();)
799 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::fillRectToRect");
800
801 SkRect croppedRect = rectToDraw;
802 SkRect croppedLocalRect = localRect;
803 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix,
804 &croppedRect, &croppedLocalRect)) {
805 return;
806 }
807
808 AutoCheckFlush acf(this->drawingManager());
809
810 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport()) {
811 InstancedRendering* ir = this->getOpList()->instancedRendering();
812 std::unique_ptr<GrDrawOp> op(ir->recordRect(croppedRect, viewMatrix, std::move(paint),
813 croppedLocalRect, aa, fInstancedPipelineInfo));
814 if (op) {
815 this->addDrawOp(clip, std::move(op));
816 return;
817 }
818 }
819
820 GrAAType aaType = this->decideAAType(aa);
821 if (GrAAType::kCoverage != aaType) {
822 this->drawNonAAFilledRect(clip, std::move(paint), viewMatrix, croppedRect,
823 &croppedLocalRect, nullptr, nullptr, aaType);
824 return;
825 }
826
827 if (view_matrix_ok_for_aa_fill_rect(viewMatrix)) {
828 std::unique_ptr<GrMeshDrawOp> op = GrAAFillRectOp::MakeWithLocalRect(
829 paint.getColor(), viewMatrix, croppedRect, croppedLocalRect);
830 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
831 this->addMeshDrawOp(pipelineBuilder, clip, std::move(op));
832 return;
833 }
834
835 SkMatrix viewAndUnLocalMatrix;
836 if (!viewAndUnLocalMatrix.setRectToRect(localRect, rectToDraw, SkMatrix::kFill_ScaleToFit)) {
837 SkDebugf("fillRectToRect called with empty local matrix.\n");
838 return;
839 }
840 viewAndUnLocalMatrix.postConcat(viewMatrix);
841
842 SkPath path;
843 path.setIsVolatile(true);
844 path.addRect(localRect);
845 this->internalDrawPath(clip, std::move(paint), aa, viewAndUnLocalMatrix, path, GrStyle());
846 }
847
fillRectWithLocalMatrix(const GrClip & clip,GrPaint && paint,GrAA aa,const SkMatrix & viewMatrix,const SkRect & rectToDraw,const SkMatrix & localMatrix)848 void GrRenderTargetContext::fillRectWithLocalMatrix(const GrClip& clip,
849 GrPaint&& paint,
850 GrAA aa,
851 const SkMatrix& viewMatrix,
852 const SkRect& rectToDraw,
853 const SkMatrix& localMatrix) {
854 ASSERT_SINGLE_OWNER
855 RETURN_IF_ABANDONED
856 SkDEBUGCODE(this->validate();)
857 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::fillRectWithLocalMatrix");
858
859 SkRect croppedRect = rectToDraw;
860 if (!crop_filled_rect(this->width(), this->height(), clip, viewMatrix, &croppedRect)) {
861 return;
862 }
863
864 AutoCheckFlush acf(this->drawingManager());
865
866 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport()) {
867 InstancedRendering* ir = this->getOpList()->instancedRendering();
868 std::unique_ptr<GrDrawOp> op(ir->recordRect(croppedRect, viewMatrix, std::move(paint),
869 localMatrix, aa, fInstancedPipelineInfo));
870 if (op) {
871 this->addDrawOp(clip, std::move(op));
872 return;
873 }
874 }
875
876 GrAAType aaType = this->decideAAType(aa);
877 if (GrAAType::kCoverage != aaType) {
878 this->drawNonAAFilledRect(clip, std::move(paint), viewMatrix, croppedRect, nullptr,
879 &localMatrix, nullptr, aaType);
880 return;
881 }
882
883 if (view_matrix_ok_for_aa_fill_rect(viewMatrix)) {
884 std::unique_ptr<GrMeshDrawOp> op =
885 GrAAFillRectOp::Make(paint.getColor(), viewMatrix, localMatrix, croppedRect);
886 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
887 this->addMeshDrawOp(pipelineBuilder, clip, std::move(op));
888 return;
889 }
890
891 SkMatrix viewAndUnLocalMatrix;
892 if (!localMatrix.invert(&viewAndUnLocalMatrix)) {
893 SkDebugf("fillRectWithLocalMatrix called with degenerate local matrix.\n");
894 return;
895 }
896 viewAndUnLocalMatrix.postConcat(viewMatrix);
897
898 SkPath path;
899 path.setIsVolatile(true);
900 path.addRect(rectToDraw);
901 path.transform(localMatrix);
902 this->internalDrawPath(clip, std::move(paint), aa, viewAndUnLocalMatrix, path, GrStyle());
903 }
904
drawVertices(const GrClip & clip,GrPaint && paint,const SkMatrix & viewMatrix,GrPrimitiveType primitiveType,int vertexCount,const SkPoint positions[],const SkPoint texCoords[],const uint32_t colors[],const uint16_t indices[],int indexCount,ColorArrayType colorArrayType)905 void GrRenderTargetContext::drawVertices(const GrClip& clip,
906 GrPaint&& paint,
907 const SkMatrix& viewMatrix,
908 GrPrimitiveType primitiveType,
909 int vertexCount,
910 const SkPoint positions[],
911 const SkPoint texCoords[],
912 const uint32_t colors[],
913 const uint16_t indices[],
914 int indexCount,
915 ColorArrayType colorArrayType) {
916 ASSERT_SINGLE_OWNER
917 RETURN_IF_ABANDONED
918 SkDEBUGCODE(this->validate();)
919 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawVertices");
920
921 AutoCheckFlush acf(this->drawingManager());
922
923 // TODO clients should give us bounds
924 SkRect bounds;
925 if (!bounds.setBoundsCheck(positions, vertexCount)) {
926 SkDebugf("drawVertices call empty bounds\n");
927 return;
928 }
929
930 std::unique_ptr<GrMeshDrawOp> op = GrDrawVerticesOp::Make(
931 paint.getColor(), primitiveType, viewMatrix, positions, vertexCount, indices,
932 indexCount, colors, texCoords, bounds, colorArrayType);
933 if (!op) {
934 return;
935 }
936 GrPipelineBuilder pipelineBuilder(std::move(paint), GrAAType::kNone);
937 this->addMeshDrawOp(pipelineBuilder, clip, std::move(op));
938 }
939
drawVertices(const GrClip & clip,GrPaint && paint,const SkMatrix & viewMatrix,sk_sp<SkVertices> vertices)940 void GrRenderTargetContext::drawVertices(const GrClip& clip,
941 GrPaint&& paint,
942 const SkMatrix& viewMatrix,
943 sk_sp<SkVertices> vertices) {
944 ASSERT_SINGLE_OWNER
945 RETURN_IF_ABANDONED
946 SkDEBUGCODE(this->validate();)
947 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawVertices");
948
949 AutoCheckFlush acf(this->drawingManager());
950
951 SkASSERT(vertices);
952 std::unique_ptr<GrMeshDrawOp> op =
953 GrDrawVerticesOp::Make(paint.getColor(), std::move(vertices), viewMatrix);
954 if (!op) {
955 return;
956 }
957 GrPipelineBuilder pipelineBuilder(std::move(paint), GrAAType::kNone);
958 this->addMeshDrawOp(pipelineBuilder, clip, std::move(op));
959 }
960
961 ///////////////////////////////////////////////////////////////////////////////
962
drawAtlas(const GrClip & clip,GrPaint && paint,const SkMatrix & viewMatrix,int spriteCount,const SkRSXform xform[],const SkRect texRect[],const SkColor colors[])963 void GrRenderTargetContext::drawAtlas(const GrClip& clip,
964 GrPaint&& paint,
965 const SkMatrix& viewMatrix,
966 int spriteCount,
967 const SkRSXform xform[],
968 const SkRect texRect[],
969 const SkColor colors[]) {
970 ASSERT_SINGLE_OWNER
971 RETURN_IF_ABANDONED
972 SkDEBUGCODE(this->validate();)
973 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawAtlas");
974
975 AutoCheckFlush acf(this->drawingManager());
976
977 std::unique_ptr<GrMeshDrawOp> op =
978 GrDrawAtlasOp::Make(paint.getColor(), viewMatrix, spriteCount, xform, texRect, colors);
979 GrPipelineBuilder pipelineBuilder(std::move(paint), GrAAType::kNone);
980 this->addMeshDrawOp(pipelineBuilder, clip, std::move(op));
981 }
982
983 ///////////////////////////////////////////////////////////////////////////////
984
drawRRect(const GrClip & origClip,GrPaint && paint,GrAA aa,const SkMatrix & viewMatrix,const SkRRect & rrect,const GrStyle & style)985 void GrRenderTargetContext::drawRRect(const GrClip& origClip,
986 GrPaint&& paint,
987 GrAA aa,
988 const SkMatrix& viewMatrix,
989 const SkRRect& rrect,
990 const GrStyle& style) {
991 ASSERT_SINGLE_OWNER
992 RETURN_IF_ABANDONED
993 SkDEBUGCODE(this->validate();)
994 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawRRect");
995 if (rrect.isEmpty()) {
996 return;
997 }
998
999 GrNoClip noclip;
1000 const GrClip* clip = &origClip;
1001 #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
1002 // The Android framework frequently clips rrects to themselves where the clip is non-aa and the
1003 // draw is aa. Since our lower level clip code works from op bounds, which are SkRects, it
1004 // doesn't detect that the clip can be ignored (modulo antialiasing). The following test
1005 // attempts to mitigate the stencil clip cost but will only help when the entire clip stack
1006 // can be ignored. We'd prefer to fix this in the framework by removing the clips calls.
1007 SkRRect devRRect;
1008 if (rrect.transform(viewMatrix, &devRRect) && clip->quickContains(devRRect)) {
1009 clip = &noclip;
1010 }
1011 #endif
1012 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
1013
1014 AutoCheckFlush acf(this->drawingManager());
1015 const SkStrokeRec stroke = style.strokeRec();
1016
1017 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport() &&
1018 stroke.isFillStyle()) {
1019 InstancedRendering* ir = this->getOpList()->instancedRendering();
1020 std::unique_ptr<GrDrawOp> op(
1021 ir->recordRRect(rrect, viewMatrix, std::move(paint), aa, fInstancedPipelineInfo));
1022 if (op) {
1023 this->addDrawOp(*clip, std::move(op));
1024 return;
1025 }
1026 }
1027
1028 GrAAType aaType = this->decideAAType(aa);
1029 if (GrAAType::kCoverage == aaType) {
1030 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
1031 std::unique_ptr<GrMeshDrawOp> op =
1032 GrOvalOpFactory::MakeRRectOp(paint.getColor(),
1033 paint.usesDistanceVectorField(),
1034 viewMatrix,
1035 rrect,
1036 stroke,
1037 shaderCaps);
1038 if (op) {
1039 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
1040 this->addMeshDrawOp(pipelineBuilder, *clip, std::move(op));
1041 return;
1042 }
1043 }
1044
1045 SkPath path;
1046 path.setIsVolatile(true);
1047 path.addRRect(rrect);
1048 this->internalDrawPath(*clip, std::move(paint), aa, viewMatrix, path, style);
1049 }
1050
1051 ///////////////////////////////////////////////////////////////////////////////
1052
drawShadowRRect(const GrClip & clip,GrPaint && paint,const SkMatrix & viewMatrix,const SkRRect & rrect,SkScalar blurRadius,const GrStyle & style)1053 void GrRenderTargetContext::drawShadowRRect(const GrClip& clip,
1054 GrPaint&& paint,
1055 const SkMatrix& viewMatrix,
1056 const SkRRect& rrect,
1057 SkScalar blurRadius,
1058 const GrStyle& style) {
1059 ASSERT_SINGLE_OWNER
1060 RETURN_IF_ABANDONED
1061 SkDEBUGCODE(this->validate();)
1062 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawShadowRRect");
1063 if (rrect.isEmpty()) {
1064 return;
1065 }
1066
1067 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
1068
1069 AutoCheckFlush acf(this->drawingManager());
1070 const SkStrokeRec stroke = style.strokeRec();
1071 // TODO: add instancing support?
1072
1073 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
1074 std::unique_ptr<GrMeshDrawOp> op = GrShadowRRectOp::Make(paint.getColor(), viewMatrix, rrect,
1075 blurRadius, stroke, shaderCaps);
1076 if (op) {
1077 GrPipelineBuilder pipelineBuilder(std::move(paint), GrAAType::kNone);
1078 this->addMeshDrawOp(pipelineBuilder, clip, std::move(op));
1079 return;
1080 }
1081 }
1082
1083 ///////////////////////////////////////////////////////////////////////////////
1084
drawFilledDRRect(const GrClip & clip,GrPaint && paint,GrAA aa,const SkMatrix & viewMatrix,const SkRRect & origOuter,const SkRRect & origInner)1085 bool GrRenderTargetContext::drawFilledDRRect(const GrClip& clip,
1086 GrPaint&& paint,
1087 GrAA aa,
1088 const SkMatrix& viewMatrix,
1089 const SkRRect& origOuter,
1090 const SkRRect& origInner) {
1091 SkASSERT(!origInner.isEmpty());
1092 SkASSERT(!origOuter.isEmpty());
1093
1094 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport()) {
1095 InstancedRendering* ir = this->getOpList()->instancedRendering();
1096 std::unique_ptr<GrDrawOp> op(ir->recordDRRect(
1097 origOuter, origInner, viewMatrix, std::move(paint), aa, fInstancedPipelineInfo));
1098 if (op) {
1099 this->addDrawOp(clip, std::move(op));
1100 return true;
1101 }
1102 }
1103
1104 GrAAType aaType = this->decideAAType(aa);
1105
1106 GrPrimitiveEdgeType innerEdgeType, outerEdgeType;
1107 if (GrAAType::kCoverage == aaType) {
1108 innerEdgeType = kInverseFillAA_GrProcessorEdgeType;
1109 outerEdgeType = kFillAA_GrProcessorEdgeType;
1110 } else {
1111 innerEdgeType = kInverseFillBW_GrProcessorEdgeType;
1112 outerEdgeType = kFillBW_GrProcessorEdgeType;
1113 }
1114
1115 SkTCopyOnFirstWrite<SkRRect> inner(origInner), outer(origOuter);
1116 SkMatrix inverseVM;
1117 if (!viewMatrix.isIdentity()) {
1118 if (!origInner.transform(viewMatrix, inner.writable())) {
1119 return false;
1120 }
1121 if (!origOuter.transform(viewMatrix, outer.writable())) {
1122 return false;
1123 }
1124 if (!viewMatrix.invert(&inverseVM)) {
1125 return false;
1126 }
1127 } else {
1128 inverseVM.reset();
1129 }
1130
1131 // TODO these need to be a geometry processors
1132 sk_sp<GrFragmentProcessor> innerEffect(GrRRectEffect::Make(innerEdgeType, *inner));
1133 if (!innerEffect) {
1134 return false;
1135 }
1136
1137 sk_sp<GrFragmentProcessor> outerEffect(GrRRectEffect::Make(outerEdgeType, *outer));
1138 if (!outerEffect) {
1139 return false;
1140 }
1141
1142 paint.addCoverageFragmentProcessor(std::move(innerEffect));
1143 paint.addCoverageFragmentProcessor(std::move(outerEffect));
1144
1145 SkRect bounds = outer->getBounds();
1146 if (GrAAType::kCoverage == aaType) {
1147 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1148 }
1149
1150 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), bounds,
1151 inverseVM);
1152 return true;
1153 }
1154
drawDRRect(const GrClip & clip,GrPaint && paint,GrAA aa,const SkMatrix & viewMatrix,const SkRRect & outer,const SkRRect & inner)1155 void GrRenderTargetContext::drawDRRect(const GrClip& clip,
1156 GrPaint&& paint,
1157 GrAA aa,
1158 const SkMatrix& viewMatrix,
1159 const SkRRect& outer,
1160 const SkRRect& inner) {
1161 ASSERT_SINGLE_OWNER
1162 RETURN_IF_ABANDONED
1163 SkDEBUGCODE(this->validate();)
1164 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawDRRect");
1165
1166 SkASSERT(!outer.isEmpty());
1167 SkASSERT(!inner.isEmpty());
1168
1169 AutoCheckFlush acf(this->drawingManager());
1170
1171 if (this->drawFilledDRRect(clip, std::move(paint), aa, viewMatrix, outer, inner)) {
1172 return;
1173 }
1174
1175 SkPath path;
1176 path.setIsVolatile(true);
1177 path.addRRect(inner);
1178 path.addRRect(outer);
1179 path.setFillType(SkPath::kEvenOdd_FillType);
1180
1181 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, GrStyle::SimpleFill());
1182 }
1183
1184 ///////////////////////////////////////////////////////////////////////////////
1185
is_int(float x)1186 static inline bool is_int(float x) {
1187 return x == (float) sk_float_round2int(x);
1188 }
1189
drawRegion(const GrClip & clip,GrPaint && paint,GrAA aa,const SkMatrix & viewMatrix,const SkRegion & region,const GrStyle & style)1190 void GrRenderTargetContext::drawRegion(const GrClip& clip,
1191 GrPaint&& paint,
1192 GrAA aa,
1193 const SkMatrix& viewMatrix,
1194 const SkRegion& region,
1195 const GrStyle& style) {
1196 ASSERT_SINGLE_OWNER
1197 RETURN_IF_ABANDONED
1198 SkDEBUGCODE(this->validate();)
1199 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawRegion");
1200
1201 if (GrAA::kYes == aa) {
1202 // GrRegionOp performs no antialiasing but is much faster, so here we check the matrix
1203 // to see whether aa is really required.
1204 if (!SkToBool(viewMatrix.getType() & ~(SkMatrix::kTranslate_Mask)) &&
1205 is_int(viewMatrix.getTranslateX()) &&
1206 is_int(viewMatrix.getTranslateY())) {
1207 aa = GrAA::kNo;
1208 }
1209 }
1210 bool complexStyle = !style.isSimpleFill();
1211 if (complexStyle || GrAA::kYes == aa) {
1212 SkPath path;
1213 region.getBoundaryPath(&path);
1214 return this->drawPath(clip, std::move(paint), aa, viewMatrix, path, style);
1215 }
1216
1217 std::unique_ptr<GrMeshDrawOp> op = GrRegionOp::Make(paint.getColor(), viewMatrix, region);
1218 GrPipelineBuilder pipelineBuilder(std::move(paint), GrAAType::kNone);
1219 this->addMeshDrawOp(pipelineBuilder, clip, std::move(op));
1220 }
1221
drawOval(const GrClip & clip,GrPaint && paint,GrAA aa,const SkMatrix & viewMatrix,const SkRect & oval,const GrStyle & style)1222 void GrRenderTargetContext::drawOval(const GrClip& clip,
1223 GrPaint&& paint,
1224 GrAA aa,
1225 const SkMatrix& viewMatrix,
1226 const SkRect& oval,
1227 const GrStyle& style) {
1228 ASSERT_SINGLE_OWNER
1229 RETURN_IF_ABANDONED
1230 SkDEBUGCODE(this->validate();)
1231 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawOval");
1232
1233 if (oval.isEmpty()) {
1234 return;
1235 }
1236
1237 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
1238
1239 AutoCheckFlush acf(this->drawingManager());
1240 const SkStrokeRec& stroke = style.strokeRec();
1241
1242 if (GrCaps::InstancedSupport::kNone != fContext->caps()->instancedSupport() &&
1243 stroke.isFillStyle()) {
1244 InstancedRendering* ir = this->getOpList()->instancedRendering();
1245 std::unique_ptr<GrDrawOp> op(
1246 ir->recordOval(oval, viewMatrix, std::move(paint), aa, fInstancedPipelineInfo));
1247 if (op) {
1248 this->addDrawOp(clip, std::move(op));
1249 return;
1250 }
1251 }
1252
1253 GrAAType aaType = this->decideAAType(aa);
1254 if (GrAAType::kCoverage == aaType) {
1255 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
1256 std::unique_ptr<GrMeshDrawOp> op =
1257 GrOvalOpFactory::MakeOvalOp(paint.getColor(), viewMatrix, oval, stroke, shaderCaps);
1258 if (op) {
1259 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
1260 this->addMeshDrawOp(pipelineBuilder, clip, std::move(op));
1261 return;
1262 }
1263 }
1264
1265 SkPath path;
1266 path.setIsVolatile(true);
1267 path.addOval(oval);
1268 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, style);
1269 }
1270
drawArc(const GrClip & clip,GrPaint && paint,GrAA aa,const SkMatrix & viewMatrix,const SkRect & oval,SkScalar startAngle,SkScalar sweepAngle,bool useCenter,const GrStyle & style)1271 void GrRenderTargetContext::drawArc(const GrClip& clip,
1272 GrPaint&& paint,
1273 GrAA aa,
1274 const SkMatrix& viewMatrix,
1275 const SkRect& oval,
1276 SkScalar startAngle,
1277 SkScalar sweepAngle,
1278 bool useCenter,
1279 const GrStyle& style) {
1280 ASSERT_SINGLE_OWNER
1281 RETURN_IF_ABANDONED
1282 SkDEBUGCODE(this->validate();)
1283 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawArc");
1284
1285 AutoCheckFlush acf(this->drawingManager());
1286
1287 GrAAType aaType = this->decideAAType(aa);
1288 if (GrAAType::kCoverage == aaType) {
1289 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
1290 std::unique_ptr<GrMeshDrawOp> op = GrOvalOpFactory::MakeArcOp(paint.getColor(),
1291 viewMatrix,
1292 oval,
1293 startAngle,
1294 sweepAngle,
1295 useCenter,
1296 style,
1297 shaderCaps);
1298 if (op) {
1299 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
1300 this->addMeshDrawOp(pipelineBuilder, clip, std::move(op));
1301 return;
1302 }
1303 }
1304 SkPath path;
1305 SkPathPriv::CreateDrawArcPath(&path, oval, startAngle, sweepAngle, useCenter,
1306 style.isSimpleFill());
1307 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, style);
1308 }
1309
drawImageLattice(const GrClip & clip,GrPaint && paint,const SkMatrix & viewMatrix,int imageWidth,int imageHeight,std::unique_ptr<SkLatticeIter> iter,const SkRect & dst)1310 void GrRenderTargetContext::drawImageLattice(const GrClip& clip,
1311 GrPaint&& paint,
1312 const SkMatrix& viewMatrix,
1313 int imageWidth,
1314 int imageHeight,
1315 std::unique_ptr<SkLatticeIter> iter,
1316 const SkRect& dst) {
1317 ASSERT_SINGLE_OWNER
1318 RETURN_IF_ABANDONED
1319 SkDEBUGCODE(this->validate();)
1320 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawImageLattice");
1321
1322 AutoCheckFlush acf(this->drawingManager());
1323
1324 std::unique_ptr<GrMeshDrawOp> op = GrLatticeOp::MakeNonAA(
1325 paint.getColor(), viewMatrix, imageWidth, imageHeight, std::move(iter), dst);
1326
1327 GrPipelineBuilder pipelineBuilder(std::move(paint), GrAAType::kNone);
1328 this->addMeshDrawOp(pipelineBuilder, clip, std::move(op));
1329 }
1330
prepareForExternalIO()1331 void GrRenderTargetContext::prepareForExternalIO() {
1332 ASSERT_SINGLE_OWNER
1333 RETURN_IF_ABANDONED
1334 SkDEBUGCODE(this->validate();)
1335 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::prepareForExternalIO");
1336
1337 // Deferral of the VRAM resources must end in this instance anyway
1338 sk_sp<GrRenderTarget> rt(
1339 sk_ref_sp(fRenderTargetProxy->instantiate(fContext->resourceProvider())));
1340 if (!rt) {
1341 return;
1342 }
1343
1344 ASSERT_OWNED_RESOURCE(rt);
1345
1346 this->drawingManager()->prepareSurfaceForExternalIO(rt.get());
1347 }
1348
drawNonAAFilledRect(const GrClip & clip,GrPaint && paint,const SkMatrix & viewMatrix,const SkRect & rect,const SkRect * localRect,const SkMatrix * localMatrix,const GrUserStencilSettings * ss,GrAAType hwOrNoneAAType)1349 void GrRenderTargetContext::drawNonAAFilledRect(const GrClip& clip,
1350 GrPaint&& paint,
1351 const SkMatrix& viewMatrix,
1352 const SkRect& rect,
1353 const SkRect* localRect,
1354 const SkMatrix* localMatrix,
1355 const GrUserStencilSettings* ss,
1356 GrAAType hwOrNoneAAType) {
1357 SkASSERT(GrAAType::kCoverage != hwOrNoneAAType);
1358 SkASSERT(hwOrNoneAAType == GrAAType::kNone || this->isStencilBufferMultisampled());
1359 std::unique_ptr<GrMeshDrawOp> op = GrRectOpFactory::MakeNonAAFill(paint.getColor(), viewMatrix,
1360 rect, localRect, localMatrix);
1361 GrPipelineBuilder pipelineBuilder(std::move(paint), hwOrNoneAAType);
1362 if (ss) {
1363 pipelineBuilder.setUserStencil(ss);
1364 }
1365 this->addMeshDrawOp(pipelineBuilder, clip, std::move(op));
1366 }
1367
1368 // Can 'path' be drawn as a pair of filled nested rectangles?
fills_as_nested_rects(const SkMatrix & viewMatrix,const SkPath & path,SkRect rects[2])1369 static bool fills_as_nested_rects(const SkMatrix& viewMatrix, const SkPath& path, SkRect rects[2]) {
1370
1371 if (path.isInverseFillType()) {
1372 return false;
1373 }
1374
1375 // TODO: this restriction could be lifted if we were willing to apply
1376 // the matrix to all the points individually rather than just to the rect
1377 if (!viewMatrix.rectStaysRect()) {
1378 return false;
1379 }
1380
1381 SkPath::Direction dirs[2];
1382 if (!path.isNestedFillRects(rects, dirs)) {
1383 return false;
1384 }
1385
1386 if (SkPath::kWinding_FillType == path.getFillType() && dirs[0] == dirs[1]) {
1387 // The two rects need to be wound opposite to each other
1388 return false;
1389 }
1390
1391 // Right now, nested rects where the margin is not the same width
1392 // all around do not render correctly
1393 const SkScalar* outer = rects[0].asScalars();
1394 const SkScalar* inner = rects[1].asScalars();
1395
1396 bool allEq = true;
1397
1398 SkScalar margin = SkScalarAbs(outer[0] - inner[0]);
1399 bool allGoE1 = margin >= SK_Scalar1;
1400
1401 for (int i = 1; i < 4; ++i) {
1402 SkScalar temp = SkScalarAbs(outer[i] - inner[i]);
1403 if (temp < SK_Scalar1) {
1404 allGoE1 = false;
1405 }
1406 if (!SkScalarNearlyEqual(margin, temp)) {
1407 allEq = false;
1408 }
1409 }
1410
1411 return allEq || allGoE1;
1412 }
1413
drawPath(const GrClip & clip,GrPaint && paint,GrAA aa,const SkMatrix & viewMatrix,const SkPath & path,const GrStyle & style)1414 void GrRenderTargetContext::drawPath(const GrClip& clip,
1415 GrPaint&& paint,
1416 GrAA aa,
1417 const SkMatrix& viewMatrix,
1418 const SkPath& path,
1419 const GrStyle& style) {
1420 ASSERT_SINGLE_OWNER
1421 RETURN_IF_ABANDONED
1422 SkDEBUGCODE(this->validate();)
1423 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::drawPath");
1424
1425 if (path.isEmpty()) {
1426 if (path.isInverseFillType()) {
1427 this->drawPaint(clip, std::move(paint), viewMatrix);
1428 }
1429 return;
1430 }
1431
1432 AutoCheckFlush acf(this->drawingManager());
1433
1434 GrAAType aaType = this->decideAAType(aa);
1435 if (GrAAType::kCoverage == aaType && !style.pathEffect()) {
1436 if (style.isSimpleFill() && !path.isConvex()) {
1437 // Concave AA paths are expensive - try to avoid them for special cases
1438 SkRect rects[2];
1439
1440 if (fills_as_nested_rects(viewMatrix, path, rects)) {
1441 std::unique_ptr<GrMeshDrawOp> op =
1442 GrRectOpFactory::MakeAAFillNestedRects(paint.getColor(), viewMatrix, rects);
1443 if (op) {
1444 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
1445 this->addMeshDrawOp(pipelineBuilder, clip, std::move(op));
1446 }
1447 return;
1448 }
1449 }
1450 SkRect ovalRect;
1451 bool isOval = path.isOval(&ovalRect);
1452
1453 if (isOval && !path.isInverseFillType()) {
1454 const GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
1455 std::unique_ptr<GrMeshDrawOp> op = GrOvalOpFactory::MakeOvalOp(
1456 paint.getColor(), viewMatrix, ovalRect, style.strokeRec(), shaderCaps);
1457 if (op) {
1458 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
1459 this->addMeshDrawOp(pipelineBuilder, clip, std::move(op));
1460 return;
1461 }
1462 }
1463 }
1464
1465 // Note that internalDrawPath may sw-rasterize the path into a scratch texture.
1466 // Scratch textures can be recycled after they are returned to the texture
1467 // cache. This presents a potential hazard for buffered drawing. However,
1468 // the writePixels that uploads to the scratch will perform a flush so we're
1469 // OK.
1470 this->internalDrawPath(clip, std::move(paint), aa, viewMatrix, path, style);
1471 }
1472
drawAndStencilPath(const GrClip & clip,const GrUserStencilSettings * ss,SkRegion::Op op,bool invert,GrAA aa,const SkMatrix & viewMatrix,const SkPath & path)1473 bool GrRenderTargetContextPriv::drawAndStencilPath(const GrClip& clip,
1474 const GrUserStencilSettings* ss,
1475 SkRegion::Op op,
1476 bool invert,
1477 GrAA aa,
1478 const SkMatrix& viewMatrix,
1479 const SkPath& path) {
1480 ASSERT_SINGLE_OWNER_PRIV
1481 RETURN_FALSE_IF_ABANDONED_PRIV
1482 SkDEBUGCODE(fRenderTargetContext->validate();)
1483 GR_AUDIT_TRAIL_AUTO_FRAME(fRenderTargetContext->fAuditTrail,
1484 "GrRenderTargetContextPriv::drawAndStencilPath");
1485
1486 if (path.isEmpty() && path.isInverseFillType()) {
1487 this->drawAndStencilRect(clip, ss, op, invert, GrAA::kNo, SkMatrix::I(),
1488 SkRect::MakeIWH(fRenderTargetContext->width(),
1489 fRenderTargetContext->height()));
1490 return true;
1491 }
1492
1493 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
1494
1495 // An Assumption here is that path renderer would use some form of tweaking
1496 // the src color (either the input alpha or in the frag shader) to implement
1497 // aa. If we have some future driver-mojo path AA that can do the right
1498 // thing WRT to the blend then we'll need some query on the PR.
1499 GrAAType aaType = fRenderTargetContext->decideAAType(aa);
1500 bool hasUserStencilSettings = !ss->isUnused();
1501
1502 GrShape shape(path, GrStyle::SimpleFill());
1503 GrPathRenderer::CanDrawPathArgs canDrawArgs;
1504 canDrawArgs.fShaderCaps =
1505 fRenderTargetContext->drawingManager()->getContext()->caps()->shaderCaps();
1506 canDrawArgs.fViewMatrix = &viewMatrix;
1507 canDrawArgs.fShape = &shape;
1508 canDrawArgs.fAAType = aaType;
1509 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
1510
1511 // Don't allow the SW renderer
1512 GrPathRenderer* pr = fRenderTargetContext->drawingManager()->getPathRenderer(
1513 canDrawArgs, false, GrPathRendererChain::DrawType::kStencilAndColor);
1514 if (!pr) {
1515 return false;
1516 }
1517
1518 GrPaint paint;
1519 paint.setCoverageSetOpXPFactory(op, invert);
1520
1521 GrPathRenderer::DrawPathArgs args{
1522 fRenderTargetContext->drawingManager()->getContext(),
1523 std::move(paint),
1524 ss,
1525 fRenderTargetContext,
1526 &clip,
1527 &viewMatrix,
1528 &shape,
1529 aaType,
1530 fRenderTargetContext->isGammaCorrect()};
1531 pr->drawPath(args);
1532 return true;
1533 }
1534
isBudgeted() const1535 SkBudgeted GrRenderTargetContextPriv::isBudgeted() const {
1536 ASSERT_SINGLE_OWNER_PRIV
1537
1538 if (fRenderTargetContext->wasAbandoned()) {
1539 return SkBudgeted::kNo;
1540 }
1541
1542 SkDEBUGCODE(fRenderTargetContext->validate();)
1543
1544 return fRenderTargetContext->fRenderTargetProxy->isBudgeted();
1545 }
1546
internalDrawPath(const GrClip & clip,GrPaint && paint,GrAA aa,const SkMatrix & viewMatrix,const SkPath & path,const GrStyle & style)1547 void GrRenderTargetContext::internalDrawPath(const GrClip& clip,
1548 GrPaint&& paint,
1549 GrAA aa,
1550 const SkMatrix& viewMatrix,
1551 const SkPath& path,
1552 const GrStyle& style) {
1553 ASSERT_SINGLE_OWNER
1554 RETURN_IF_ABANDONED
1555 SkASSERT(!path.isEmpty());
1556 GrShape shape;
1557
1558 GrAAType aaType = this->decideAAType(aa, /*allowMixedSamples*/ true);
1559 if (style.isSimpleHairline() && aaType == GrAAType::kMixedSamples) {
1560 // NVPR cannot handle hairlines, so this will would get picked up by a different stencil and
1561 // cover path renderer (i.e. default path renderer). The hairline renderer produces much
1562 // smoother hairlines than MSAA.
1563 aaType = GrAAType::kCoverage;
1564 }
1565 GrPathRenderer::CanDrawPathArgs canDrawArgs;
1566 canDrawArgs.fShaderCaps = this->drawingManager()->getContext()->caps()->shaderCaps();
1567 canDrawArgs.fViewMatrix = &viewMatrix;
1568 canDrawArgs.fShape = &shape;
1569 canDrawArgs.fHasUserStencilSettings = false;
1570
1571 GrPathRenderer* pr;
1572 static constexpr GrPathRendererChain::DrawType kType = GrPathRendererChain::DrawType::kColor;
1573 do {
1574 shape = GrShape(path, style);
1575 if (shape.isEmpty()) {
1576 return;
1577 }
1578
1579 canDrawArgs.fAAType = aaType;
1580
1581 // Try a 1st time without applying any of the style to the geometry (and barring sw)
1582 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
1583 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
1584
1585 if (!pr && shape.style().pathEffect()) {
1586 // It didn't work above, so try again with the path effect applied.
1587 shape = shape.applyStyle(GrStyle::Apply::kPathEffectOnly, styleScale);
1588 if (shape.isEmpty()) {
1589 return;
1590 }
1591 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
1592 }
1593 if (!pr) {
1594 if (shape.style().applies()) {
1595 shape = shape.applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, styleScale);
1596 if (shape.isEmpty()) {
1597 return;
1598 }
1599 }
1600 // This time, allow SW renderer
1601 pr = this->drawingManager()->getPathRenderer(canDrawArgs, true, kType);
1602 }
1603 if (!pr && GrAATypeIsHW(aaType)) {
1604 // There are exceptional cases where we may wind up falling back to coverage based AA
1605 // when the target is MSAA (e.g. through disabling path renderers via GrContextOptions).
1606 aaType = GrAAType::kCoverage;
1607 } else {
1608 break;
1609 }
1610 } while(true);
1611
1612 if (!pr) {
1613 #ifdef SK_DEBUG
1614 SkDebugf("Unable to find path renderer compatible with path.\n");
1615 #endif
1616 return;
1617 }
1618
1619 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
1620 std::move(paint),
1621 &GrUserStencilSettings::kUnused,
1622 this,
1623 &clip,
1624 &viewMatrix,
1625 &shape,
1626 aaType,
1627 this->isGammaCorrect()};
1628 pr->drawPath(args);
1629 }
1630
op_bounds(SkRect * bounds,const GrOp * op)1631 static void op_bounds(SkRect* bounds, const GrOp* op) {
1632 *bounds = op->bounds();
1633 if (op->hasZeroArea()) {
1634 if (op->hasAABloat()) {
1635 bounds->outset(0.5f, 0.5f);
1636 } else {
1637 // We don't know which way the particular GPU will snap lines or points at integer
1638 // coords. So we ensure that the bounds is large enough for either snap.
1639 SkRect before = *bounds;
1640 bounds->roundOut(bounds);
1641 if (bounds->fLeft == before.fLeft) {
1642 bounds->fLeft -= 1;
1643 }
1644 if (bounds->fTop == before.fTop) {
1645 bounds->fTop -= 1;
1646 }
1647 if (bounds->fRight == before.fRight) {
1648 bounds->fRight += 1;
1649 }
1650 if (bounds->fBottom == before.fBottom) {
1651 bounds->fBottom += 1;
1652 }
1653 }
1654 }
1655 }
1656
addDrawOp(const GrClip & clip,std::unique_ptr<GrDrawOp> op)1657 uint32_t GrRenderTargetContext::addDrawOp(const GrClip& clip, std::unique_ptr<GrDrawOp> op) {
1658 ASSERT_SINGLE_OWNER
1659 if (this->drawingManager()->wasAbandoned()) {
1660 return SK_InvalidUniqueID;
1661 }
1662 SkDEBUGCODE(this->validate();)
1663 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::addDrawOp");
1664
1665 // Setup clip
1666 SkRect bounds;
1667 op_bounds(&bounds, op.get());
1668 GrAppliedClip appliedClip;
1669 GrDrawOp::FixedFunctionFlags fixedFunctionFlags = op->fixedFunctionFlags();
1670 if (!clip.apply(fContext, this, fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesHWAA,
1671 fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil, &appliedClip,
1672 &bounds)) {
1673 return SK_InvalidUniqueID;
1674 }
1675
1676 // This forces instantiation of the render target.
1677 GrRenderTarget* rt = this->accessRenderTarget();
1678 if (!rt) {
1679 return SK_InvalidUniqueID;
1680 }
1681
1682 if (fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil ||
1683 appliedClip.hasStencilClip()) {
1684 if (!fContext->resourceProvider()->attachStencilAttachment(rt)) {
1685 SkDebugf("ERROR creating stencil attachment. Draw skipped.\n");
1686 return SK_InvalidUniqueID;
1687 }
1688 }
1689
1690 GrXferProcessor::DstTexture dstTexture;
1691 if (op->xpRequiresDstTexture(*this->caps(), &appliedClip)) {
1692 this->setupDstTexture(rt, clip, op->bounds(), &dstTexture);
1693 if (!dstTexture.texture()) {
1694 return SK_InvalidUniqueID;
1695 }
1696 }
1697
1698 op->setClippedBounds(bounds);
1699 return this->getOpList()->addOp(std::move(op), this, std::move(appliedClip), dstTexture);
1700 }
1701
addMeshDrawOp(const GrPipelineBuilder & pipelineBuilder,const GrClip & clip,std::unique_ptr<GrMeshDrawOp> op)1702 uint32_t GrRenderTargetContext::addMeshDrawOp(const GrPipelineBuilder& pipelineBuilder,
1703 const GrClip& clip,
1704 std::unique_ptr<GrMeshDrawOp> op) {
1705 ASSERT_SINGLE_OWNER
1706 if (this->drawingManager()->wasAbandoned()) {
1707 return SK_InvalidUniqueID;
1708 }
1709 SkDEBUGCODE(this->validate();)
1710 GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrRenderTargetContext::addMeshDrawOp");
1711
1712 // Setup clip
1713 SkRect bounds;
1714 op_bounds(&bounds, op.get());
1715 GrAppliedClip appliedClip;
1716 if (!clip.apply(fContext, this, pipelineBuilder.isHWAntialias(),
1717 pipelineBuilder.hasUserStencilSettings(), &appliedClip, &bounds)) {
1718 return SK_InvalidUniqueID;
1719 }
1720
1721 // This forces instantiation of the render target. Pipeline creation is moving to flush time
1722 // by which point instantiation must have occurred anyway.
1723 GrRenderTarget* rt = this->accessRenderTarget();
1724 if (!rt) {
1725 return SK_InvalidUniqueID;
1726 }
1727
1728 GrResourceProvider* resourceProvider = fContext->resourceProvider();
1729 if (pipelineBuilder.hasUserStencilSettings() || appliedClip.hasStencilClip()) {
1730 if (!resourceProvider->attachStencilAttachment(this->accessRenderTarget())) {
1731 SkDebugf("ERROR creating stencil attachment. Draw skipped.\n");
1732 return SK_InvalidUniqueID;
1733 }
1734 }
1735
1736 GrProcessorSet::FragmentProcessorAnalysis analysis;
1737 op->analyzeProcessors(&analysis, pipelineBuilder.processors(), &appliedClip, *this->caps());
1738
1739 GrPipeline::InitArgs args;
1740 pipelineBuilder.getPipelineInitArgs(&args);
1741 args.fAppliedClip = &appliedClip;
1742 args.fRenderTarget = rt;
1743 args.fCaps = this->caps();
1744 args.fAnalysis = &analysis;
1745
1746 if (pipelineBuilder.willXPNeedDstTexture(*this->caps(), analysis)) {
1747 this->setupDstTexture(rt, clip, bounds, &args.fDstTexture);
1748 if (!args.fDstTexture.texture()) {
1749 return SK_InvalidUniqueID;
1750 }
1751 }
1752 op->initPipeline(args);
1753 // TODO: We need to add pipeline dependencies on textures, etc before recording this op.
1754 op->setClippedBounds(bounds);
1755 return this->getOpList()->addOp(std::move(op), this);
1756 }
1757
setupDstTexture(GrRenderTarget * rt,const GrClip & clip,const SkRect & opBounds,GrXferProcessor::DstTexture * dstTexture)1758 void GrRenderTargetContext::setupDstTexture(GrRenderTarget* rt, const GrClip& clip,
1759 const SkRect& opBounds,
1760 GrXferProcessor::DstTexture* dstTexture) {
1761 if (this->caps()->textureBarrierSupport()) {
1762 if (GrTexture* rtTex = rt->asTexture()) {
1763 // The render target is a texture, so we can read from it directly in the shader. The XP
1764 // will be responsible to detect this situation and request a texture barrier.
1765 dstTexture->setTexture(sk_ref_sp(rtTex));
1766 dstTexture->setOffset(0, 0);
1767 return;
1768 }
1769 }
1770
1771 SkIRect copyRect;
1772 clip.getConservativeBounds(rt->width(), rt->height(), ©Rect);
1773
1774 SkIRect drawIBounds;
1775 opBounds.roundOut(&drawIBounds);
1776 // Cover up for any precision issues by outsetting the op bounds a pixel in each direction.
1777 drawIBounds.outset(1, 1);
1778 if (!copyRect.intersect(drawIBounds)) {
1779 #ifdef SK_DEBUG
1780 GrCapsDebugf(this->caps(), "Missed an early reject. "
1781 "Bailing on draw from setupDstTexture.\n");
1782 #endif
1783 return;
1784 }
1785
1786 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
1787 // have per-sample dst values by making the copy multisampled.
1788 GrSurfaceDesc desc;
1789 if (!this->caps()->initDescForDstCopy(rt, &desc)) {
1790 desc.fOrigin = kDefault_GrSurfaceOrigin;
1791 desc.fFlags = kRenderTarget_GrSurfaceFlag;
1792 desc.fConfig = rt->config();
1793 }
1794
1795 desc.fWidth = copyRect.width();
1796 desc.fHeight = copyRect.height();
1797
1798 static const uint32_t kFlags = 0;
1799 sk_sp<GrTexture> copy(fContext->resourceProvider()->createApproxTexture(desc, kFlags));
1800
1801 if (!copy) {
1802 SkDebugf("Failed to create temporary copy of destination texture.\n");
1803 return;
1804 }
1805 SkIPoint dstPoint = {0, 0};
1806 this->getOpList()->copySurface(copy.get(), rt, copyRect, dstPoint);
1807 dstTexture->setTexture(std::move(copy));
1808 dstTexture->setOffset(copyRect.fLeft, copyRect.fTop);
1809 }
1810