blob: 8c9dde745c19c540647017a2d164f8135cd501f6 [file] [log] [blame]
robertphillipsea461502015-05-26 11:38:03 -07001/*
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
Brian Salomonf19f9ca2019-09-18 15:54:26 -04008#include "src/gpu/GrRenderTargetContext.h"
9
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkDrawable.h"
11#include "include/gpu/GrBackendSemaphore.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/private/GrRecordingContext.h"
13#include "include/private/SkShadowFlags.h"
14#include "include/utils/SkShadowUtils.h"
Brian Salomoncd734f62019-05-10 16:32:54 -040015#include "src/core/SkAutoPixmapStorage.h"
16#include "src/core/SkConvertPixels.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/core/SkDrawShadowInfo.h"
18#include "src/core/SkGlyphRunPainter.h"
19#include "src/core/SkLatticeIter.h"
Brian Salomon9241a6d2019-10-03 13:26:54 -040020#include "src/core/SkMakeUnique.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/core/SkMatrixPriv.h"
22#include "src/core/SkRRectPriv.h"
23#include "src/core/SkSurfacePriv.h"
Brian Osman28ba5282019-10-30 14:18:07 -040024#include "src/core/SkYUVMath.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/GrAppliedClip.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040026#include "src/gpu/GrAuditTrail.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/gpu/GrBlurUtils.h"
28#include "src/gpu/GrCaps.h"
Brian Salomon9241a6d2019-10-03 13:26:54 -040029#include "src/gpu/GrClientMappedBufferManager.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040030#include "src/gpu/GrColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050031#include "src/gpu/GrContextPriv.h"
Brian Salomonf30b1c12019-06-20 12:25:02 -040032#include "src/gpu/GrDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050033#include "src/gpu/GrDrawingManager.h"
34#include "src/gpu/GrFixedClip.h"
35#include "src/gpu/GrGpuResourcePriv.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040036#include "src/gpu/GrImageInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050037#include "src/gpu/GrMemoryPool.h"
38#include "src/gpu/GrPathRenderer.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050039#include "src/gpu/GrRecordingContextPriv.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040040#include "src/gpu/GrRenderTarget.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050041#include "src/gpu/GrRenderTargetContextPriv.h"
42#include "src/gpu/GrResourceProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050043#include "src/gpu/GrStencilAttachment.h"
44#include "src/gpu/GrStyle.h"
45#include "src/gpu/GrTracing.h"
46#include "src/gpu/SkGr.h"
Brian Salomon031b0ba2019-05-23 11:05:26 -040047#include "src/gpu/effects/GrBicubicEffect.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050048#include "src/gpu/effects/GrRRectEffect.h"
49#include "src/gpu/effects/GrTextureDomain.h"
Brian Salomon024bd002019-06-11 11:38:16 -040050#include "src/gpu/effects/generated/GrColorMatrixFragmentProcessor.h"
Michael Ludwigfd4f4df2019-05-29 09:51:09 -040051#include "src/gpu/geometry/GrQuad.h"
Michael Ludwig61328202019-06-19 14:48:58 +000052#include "src/gpu/geometry/GrQuadUtils.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040053#include "src/gpu/geometry/GrShape.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050054#include "src/gpu/ops/GrAtlasTextOp.h"
55#include "src/gpu/ops/GrClearOp.h"
56#include "src/gpu/ops/GrClearStencilClipOp.h"
57#include "src/gpu/ops/GrDebugMarkerOp.h"
58#include "src/gpu/ops/GrDrawAtlasOp.h"
59#include "src/gpu/ops/GrDrawOp.h"
60#include "src/gpu/ops/GrDrawVerticesOp.h"
61#include "src/gpu/ops/GrDrawableOp.h"
62#include "src/gpu/ops/GrFillRRectOp.h"
63#include "src/gpu/ops/GrFillRectOp.h"
64#include "src/gpu/ops/GrLatticeOp.h"
65#include "src/gpu/ops/GrOp.h"
66#include "src/gpu/ops/GrOvalOpFactory.h"
67#include "src/gpu/ops/GrRegionOp.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050068#include "src/gpu/ops/GrShadowRRectOp.h"
69#include "src/gpu/ops/GrStencilPathOp.h"
70#include "src/gpu/ops/GrStrokeRectOp.h"
71#include "src/gpu/ops/GrTextureOp.h"
72#include "src/gpu/text/GrTextContext.h"
73#include "src/gpu/text/GrTextTarget.h"
Brian Salomonf18b1d82017-10-27 11:30:49 -040074
Herb Derbyc1b482c2018-08-09 15:02:27 -040075class GrRenderTargetContext::TextTarget : public GrTextTarget {
Brian Salomonf18b1d82017-10-27 11:30:49 -040076public:
77 TextTarget(GrRenderTargetContext* renderTargetContext)
Herb Derbyc1b482c2018-08-09 15:02:27 -040078 : GrTextTarget(renderTargetContext->width(), renderTargetContext->height(),
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040079 renderTargetContext->colorInfo())
Herb Derby74c6ed32018-07-28 18:07:54 -040080 , fRenderTargetContext(renderTargetContext)
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040081 , fGlyphPainter{*renderTargetContext} {}
Brian Salomonf18b1d82017-10-27 11:30:49 -040082
Robert Phillips7c525e62018-06-12 10:11:12 -040083 void addDrawOp(const GrClip& clip, std::unique_ptr<GrAtlasTextOp> op) override {
Brian Salomonf18b1d82017-10-27 11:30:49 -040084 fRenderTargetContext->addDrawOp(clip, std::move(op));
85 }
86
Robert Phillips46a13382018-08-23 13:53:01 -040087 void drawShape(const GrClip& clip, const SkPaint& paint,
88 const SkMatrix& viewMatrix, const GrShape& shape) override {
Robert Phillips27927a52018-08-20 13:18:12 -040089 GrBlurUtils::drawShapeWithMaskFilter(fRenderTargetContext->fContext, fRenderTargetContext,
90 clip, paint, viewMatrix, shape);
Brian Salomonf18b1d82017-10-27 11:30:49 -040091 }
92
93 void makeGrPaint(GrMaskFormat maskFormat, const SkPaint& skPaint, const SkMatrix& viewMatrix,
Robert Phillips7c525e62018-06-12 10:11:12 -040094 GrPaint* grPaint) override {
Robert Phillips69893702019-02-22 11:16:30 -050095 auto context = fRenderTargetContext->fContext;
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040096 const GrColorInfo& colorInfo = fRenderTargetContext->colorInfo();
Brian Salomonf18b1d82017-10-27 11:30:49 -040097 if (kARGB_GrMaskFormat == maskFormat) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040098 SkPaintToGrPaintWithPrimitiveColor(context, colorInfo, skPaint, grPaint);
Brian Salomonf18b1d82017-10-27 11:30:49 -040099 } else {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400100 SkPaintToGrPaint(context, colorInfo, skPaint, viewMatrix, grPaint);
Brian Salomonf18b1d82017-10-27 11:30:49 -0400101 }
102 }
103
Robert Phillips69893702019-02-22 11:16:30 -0500104 GrRecordingContext* getContext() override {
Robert Phillips7c525e62018-06-12 10:11:12 -0400105 return fRenderTargetContext->fContext;
106 }
107
Herb Derby65956872018-08-21 16:55:04 -0400108 SkGlyphRunListPainter* glyphPainter() override {
109 return &fGlyphPainter;
Herb Derby74c6ed32018-07-28 18:07:54 -0400110 }
111
Brian Salomonf18b1d82017-10-27 11:30:49 -0400112private:
113 GrRenderTargetContext* fRenderTargetContext;
Herb Derby65956872018-08-21 16:55:04 -0400114 SkGlyphRunListPainter fGlyphPainter;
Herb Derby74c6ed32018-07-28 18:07:54 -0400115
Brian Salomonf18b1d82017-10-27 11:30:49 -0400116};
joshualittbc907352016-01-13 06:45:40 -0800117
Robert Phillips72152832017-01-25 17:31:35 -0500118#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this->drawingManager()->getContext())
joshualitt1de610a2016-01-06 08:26:09 -0800119#define ASSERT_SINGLE_OWNER \
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400120 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());)
robertphillips391395d2016-03-02 09:26:36 -0800121#define ASSERT_SINGLE_OWNER_PRIV \
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400122 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fRenderTargetContext->singleOwner());)
Robert Phillips69893702019-02-22 11:16:30 -0500123#define RETURN_IF_ABANDONED if (fContext->priv().abandoned()) { return; }
124#define RETURN_IF_ABANDONED_PRIV if (fRenderTargetContext->fContext->priv().abandoned()) { return; }
125#define RETURN_FALSE_IF_ABANDONED if (fContext->priv().abandoned()) { return false; }
126#define RETURN_FALSE_IF_ABANDONED_PRIV if (fRenderTargetContext->fContext->priv().abandoned()) { return false; }
127#define RETURN_NULL_IF_ABANDONED if (fContext->priv().abandoned()) { return nullptr; }
robertphillipsea461502015-05-26 11:38:03 -0700128
Brian Salomone225b562017-06-14 13:00:03 -0400129//////////////////////////////////////////////////////////////////////////////
130
robertphillipsea461502015-05-26 11:38:03 -0700131class AutoCheckFlush {
132public:
halcanary9d524f22016-03-29 09:03:52 -0700133 AutoCheckFlush(GrDrawingManager* drawingManager) : fDrawingManager(drawingManager) {
robertphillips77a2e522015-10-17 07:43:27 -0700134 SkASSERT(fDrawingManager);
135 }
bsalomonb77a9072016-09-07 10:02:04 -0700136 ~AutoCheckFlush() { fDrawingManager->flushIfNecessary(); }
robertphillipsea461502015-05-26 11:38:03 -0700137
138private:
robertphillips77a2e522015-10-17 07:43:27 -0700139 GrDrawingManager* fDrawingManager;
robertphillipsea461502015-05-26 11:38:03 -0700140};
141
Greg Danielf41b2bd2019-08-22 16:19:24 -0400142// In MDB mode the reffing of the 'getLastOpsTask' call's result allows in-progress
143// GrOpsTask to be picked up and added to by renderTargetContexts lower in the call
144// stack. When this occurs with a closed GrOpsTask, a new one will be allocated
145// when the renderTargetContext attempts to use it (via getOpsTask).
Robert Phillips69893702019-02-22 11:16:30 -0500146GrRenderTargetContext::GrRenderTargetContext(GrRecordingContext* context,
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400147 sk_sp<GrRenderTargetProxy> rtp,
Brian Salomond6287472019-06-24 15:50:07 -0400148 GrColorType colorType,
Greg Daniela83de582019-10-22 09:33:25 -0400149 GrSurfaceOrigin origin,
150 GrSwizzle texSwizzle,
151 GrSwizzle outSwizzle,
Brian Osman11052242016-10-27 14:47:55 -0400152 sk_sp<SkColorSpace> colorSpace,
153 const SkSurfaceProps* surfaceProps,
Greg Danielf41b2bd2019-08-22 16:19:24 -0400154 bool managedOpsTask)
Greg Daniel901b98e2019-10-22 09:54:02 -0400155 : GrSurfaceContext(context, colorType, kPremul_SkAlphaType, std::move(colorSpace), origin,
156 texSwizzle)
Brian Salomonf3569f02017-10-24 12:52:33 -0400157 , fRenderTargetProxy(std::move(rtp))
Greg Daniela83de582019-10-22 09:33:25 -0400158 , fOutputSwizzle(outSwizzle)
Greg Danielf41b2bd2019-08-22 16:19:24 -0400159 , fOpsTask(sk_ref_sp(fRenderTargetProxy->getLastOpsTask()))
Brian Salomonf3569f02017-10-24 12:52:33 -0400160 , fSurfaceProps(SkSurfacePropsCopyOrDefault(surfaceProps))
Greg Danielf41b2bd2019-08-22 16:19:24 -0400161 , fManagedOpsTask(managedOpsTask) {
Brian Salomonf18b1d82017-10-27 11:30:49 -0400162 fTextTarget.reset(new TextTarget(this));
robertphillips2e1e51f2015-10-15 08:01:48 -0700163 SkDEBUGCODE(this->validate();)
robertphillipsea461502015-05-26 11:38:03 -0700164}
165
robertphillips2e1e51f2015-10-15 08:01:48 -0700166#ifdef SK_DEBUG
Brian Osman11052242016-10-27 14:47:55 -0400167void GrRenderTargetContext::validate() const {
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400168 SkASSERT(fRenderTargetProxy);
169 fRenderTargetProxy->validate(fContext);
robertphillipsa106c622015-10-16 09:07:06 -0700170
Greg Daniel836080f2019-10-15 14:32:02 -0400171 SkASSERT(fContext->priv().caps()->areColorTypeAndFormatCompatible(
172 this->colorInfo().colorType(), fRenderTargetProxy->backendFormat()));
173
Greg Danielf41b2bd2019-08-22 16:19:24 -0400174 if (fOpsTask && !fOpsTask->isClosed()) {
175 SkASSERT(fRenderTargetProxy->getLastRenderTask() == fOpsTask.get());
robertphillipsa106c622015-10-16 09:07:06 -0700176 }
robertphillips2e1e51f2015-10-15 08:01:48 -0700177}
178#endif
179
Brian Osman11052242016-10-27 14:47:55 -0400180GrRenderTargetContext::~GrRenderTargetContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800181 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700182}
183
Chris Dalton7d6748e2019-03-13 00:34:52 -0600184inline GrAAType GrRenderTargetContext::chooseAAType(GrAA aa) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600185 if (GrAA::kNo == aa) {
186 // On some devices we cannot disable MSAA if it is enabled so we make the AA type reflect
187 // that.
Chris Dalton6ce447a2019-06-23 18:07:38 -0600188 if (this->numSamples() > 1 && !this->caps()->multisampleDisableSupport()) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600189 return GrAAType::kMSAA;
190 }
191 return GrAAType::kNone;
192 }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600193 return (this->numSamples() > 1) ? GrAAType::kMSAA : GrAAType::kCoverage;
Chris Dalton7d6748e2019-03-13 00:34:52 -0600194}
195
Robert Phillipsf200a902017-01-30 13:27:37 -0500196GrTextureProxy* GrRenderTargetContext::asTextureProxy() {
Robert Phillipseaa86252016-11-08 13:49:39 +0000197 return fRenderTargetProxy->asTextureProxy();
198}
199
Greg Daniele252f082017-10-23 16:05:23 -0400200const GrTextureProxy* GrRenderTargetContext::asTextureProxy() const {
201 return fRenderTargetProxy->asTextureProxy();
202}
203
Robert Phillipsf200a902017-01-30 13:27:37 -0500204sk_sp<GrTextureProxy> GrRenderTargetContext::asTextureProxyRef() {
205 return sk_ref_sp(fRenderTargetProxy->asTextureProxy());
206}
207
Greg Daniele252f082017-10-23 16:05:23 -0400208GrMipMapped GrRenderTargetContext::mipMapped() const {
209 if (const GrTextureProxy* proxy = this->asTextureProxy()) {
210 return proxy->mipMapped();
211 }
212 return GrMipMapped::kNo;
213}
214
Greg Danielf41b2bd2019-08-22 16:19:24 -0400215GrOpsTask* GrRenderTargetContext::getOpsTask() {
joshualitt1de610a2016-01-06 08:26:09 -0800216 ASSERT_SINGLE_OWNER
robertphillipsa106c622015-10-16 09:07:06 -0700217 SkDEBUGCODE(this->validate();)
218
Greg Danielf41b2bd2019-08-22 16:19:24 -0400219 if (!fOpsTask || fOpsTask->isClosed()) {
Chris Dalton674f77a2019-09-30 20:49:39 -0600220 sk_sp<GrOpsTask> newOpsTask =
Greg Daniel16f5c652019-10-29 11:26:01 -0400221 this->drawingManager()->newOpsTask(this->outputSurfaceView(), fManagedOpsTask);
Chris Daltondec74f32019-10-02 02:32:37 -0600222 if (fOpsTask && fNumStencilSamples > 0) {
223 // Store the stencil values in memory upon completion of fOpsTask.
Chris Dalton674f77a2019-09-30 20:49:39 -0600224 fOpsTask->setMustPreserveStencil();
225 // Reload the stencil buffer content at the beginning of newOpsTask.
226 // FIXME: Could the topo sort insert a task between these two that modifies the stencil
227 // values?
228 newOpsTask->setInitialStencilContent(GrOpsTask::StencilContent::kPreserved);
229 }
230 fOpsTask = std::move(newOpsTask);
robertphillipsa106c622015-10-16 09:07:06 -0700231 }
232
Greg Danielf41b2bd2019-08-22 16:19:24 -0400233 return fOpsTask.get();
robertphillipsea461502015-05-26 11:38:03 -0700234}
235
Herb Derbycddab252018-07-16 11:19:04 -0400236void GrRenderTargetContext::drawGlyphRunList(
237 const GrClip& clip, const SkMatrix& viewMatrix,
Robert Phillipse4643cc2018-08-14 13:01:29 -0400238 const SkGlyphRunList& blob) {
joshualitt1de610a2016-01-06 08:26:09 -0800239 ASSERT_SINGLE_OWNER
robertphillips2d70dcb2015-10-06 07:38:23 -0700240 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700241 SkDEBUGCODE(this->validate();)
Herb Derbycddab252018-07-16 11:19:04 -0400242 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawGlyphRunList", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -0700243
Greg Danielbe7fc462019-01-03 16:40:42 -0500244 // Drawing text can cause us to do inline uploads. This is not supported for wrapped vulkan
245 // secondary command buffers because it would require stopping and starting a render pass which
246 // we don't have access to.
247 if (this->wrapsVkSecondaryCB()) {
248 return;
249 }
250
Herb Derby26cbe512018-05-24 14:39:01 -0400251 GrTextContext* atlasTextContext = this->drawingManager()->getTextContext();
Herb Derbycddab252018-07-16 11:19:04 -0400252 atlasTextContext->drawGlyphRunList(fContext, fTextTarget.get(), clip, viewMatrix,
Robert Phillipse4643cc2018-08-14 13:01:29 -0400253 fSurfaceProps, blob);
robertphillipsea461502015-05-26 11:38:03 -0700254}
255
Brian Osman11052242016-10-27 14:47:55 -0400256void GrRenderTargetContext::discard() {
joshualitt1de610a2016-01-06 08:26:09 -0800257 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700258 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700259 SkDEBUGCODE(this->validate();)
Robert Phillips6b47c7d2017-08-29 07:24:09 -0400260 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "discard", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700261
Robert Phillips72152832017-01-25 17:31:35 -0500262 AutoCheckFlush acf(this->drawingManager());
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400263
Greg Danielf41b2bd2019-08-22 16:19:24 -0400264 this->getOpsTask()->discard();
robertphillipsea461502015-05-26 11:38:03 -0700265}
266
Brian Osman11052242016-10-27 14:47:55 -0400267void GrRenderTargetContext::clear(const SkIRect* rect,
Brian Osman9a9baae2018-11-05 15:06:26 -0500268 const SkPMColor4f& color,
Chris Dalton344e9032017-12-11 15:42:09 -0700269 CanClearFullscreen canClearFullscreen) {
joshualitt1de610a2016-01-06 08:26:09 -0800270 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700271 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700272 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400273 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "clear", fContext);
robertphillipsea461502015-05-26 11:38:03 -0700274
Robert Phillips72152832017-01-25 17:31:35 -0500275 AutoCheckFlush acf(this->drawingManager());
Chris Dalton344e9032017-12-11 15:42:09 -0700276 this->internalClear(rect ? GrFixedClip(*rect) : GrFixedClip::Disabled(), color,
277 canClearFullscreen);
csmartdalton29df7602016-08-31 11:55:52 -0700278}
robertphillips9199a9f2016-07-13 07:48:43 -0700279
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500280void GrRenderTargetContextPriv::clear(const GrFixedClip& clip,
281 const SkPMColor4f& color,
282 CanClearFullscreen canClearFullscreen) {
283 ASSERT_SINGLE_OWNER_PRIV
284 RETURN_IF_ABANDONED_PRIV
285 SkDEBUGCODE(fRenderTargetContext->validate();)
286 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clear",
287 fRenderTargetContext->fContext);
288
289 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
290 fRenderTargetContext->internalClear(clip, color, canClearFullscreen);
291}
292
293static void clear_to_grpaint(const SkPMColor4f& color, GrPaint* paint) {
294 paint->setColor4f(color);
295 if (color.isOpaque()) {
296 // Can just rely on the src-over blend mode to do the right thing
297 paint->setPorterDuffXPFactory(SkBlendMode::kSrcOver);
298 } else {
299 // A clear overwrites the prior color, so even if it's transparent, it behaves as if it
300 // were src blended
301 paint->setPorterDuffXPFactory(SkBlendMode::kSrc);
302 }
303}
304
305void GrRenderTargetContext::internalClear(const GrFixedClip& clip,
306 const SkPMColor4f& color,
307 CanClearFullscreen canClearFullscreen) {
308 bool isFull = false;
309 if (!clip.hasWindowRectangles()) {
Robert Phillips0e35ce22019-04-05 10:57:28 -0400310 // TODO: wrt the shouldInitializeTextures path, it would be more performant to
311 // only clear the entire target if we knew it had not been cleared before. As
312 // is this could end up doing a lot of redundant clears.
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500313 isFull = !clip.scissorEnabled() ||
314 (CanClearFullscreen::kYes == canClearFullscreen &&
Robert Phillips0e35ce22019-04-05 10:57:28 -0400315 (this->caps()->preferFullscreenClears() || this->caps()->shouldInitializeTextures())) ||
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500316 clip.scissorRect().contains(SkIRect::MakeWH(this->width(), this->height()));
317 }
318
319 if (isFull) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400320 GrOpsTask* opsTask = this->getOpsTask();
321 if (opsTask->resetForFullscreenClear(this->canDiscardPreviousOpsOnFullClear()) &&
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500322 !this->caps()->performColorClearsAsDraws()) {
323 // The op list was emptied and native clears are allowed, so just use the load op
Greg Danielf41b2bd2019-08-22 16:19:24 -0400324 opsTask->setColorLoadOp(GrLoadOp::kClear, color);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500325 return;
326 } else {
327 // Will use an op for the clear, reset the load op to discard since the op will
328 // blow away the color buffer contents
Greg Danielf41b2bd2019-08-22 16:19:24 -0400329 opsTask->setColorLoadOp(GrLoadOp::kDiscard);
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500330 }
331
332 // Must add an op to the list (either because we couldn't use a load op, or because the
333 // clear load op isn't supported)
334 if (this->caps()->performColorClearsAsDraws()) {
335 SkRect rtRect = SkRect::MakeWH(this->width(), this->height());
336 GrPaint paint;
337 clear_to_grpaint(color, &paint);
338 this->addDrawOp(GrFixedClip::Disabled(),
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400339 GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
340 rtRect));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500341 } else {
Chris Dalton08755122019-08-05 16:13:47 -0600342 this->addOp(GrClearOp::Make(
343 fContext, SkIRect::MakeEmpty(), color, /* fullscreen */ true));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500344 }
345 } else {
346 if (this->caps()->performPartialClearsAsDraws()) {
347 // performPartialClearsAsDraws() also returns true if any clear has to be a draw.
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500348 GrPaint paint;
349 clear_to_grpaint(color, &paint);
350
Michael Ludwig64b28a72019-05-28 12:02:00 -0400351 this->addDrawOp(clip,
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400352 GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
353 SkRect::Make(clip.scissorRect())));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500354 } else {
355 std::unique_ptr<GrOp> op(GrClearOp::Make(fContext, clip, color,
356 this->asSurfaceProxy()));
357 // This version of the clear op factory can return null if the clip doesn't intersect
358 // with the surface proxy's boundary
359 if (!op) {
360 return;
361 }
Chris Dalton08755122019-08-05 16:13:47 -0600362 this->addOp(std::move(op));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500363 }
364 }
365}
366
Brian Osman11052242016-10-27 14:47:55 -0400367void GrRenderTargetContext::drawPaint(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500368 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400369 const SkMatrix& viewMatrix) {
Michael Ludwig61328202019-06-19 14:48:58 +0000370 // Start with the render target, since that is the maximum content we could possibly fill.
371 // drawFilledQuad() will automatically restrict it to clip bounds for us if possible.
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400372 SkRect r = fRenderTargetProxy->getBoundsRect();
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400373 if (!paint.numTotalFragmentProcessors()) {
Michael Ludwig61328202019-06-19 14:48:58 +0000374 // The paint is trivial so we won't need to use local coordinates, so skip calculating the
375 // inverse view matrix.
376 this->fillRectToRect(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r, r);
377 } else {
378 // Use the inverse view matrix to arrive at appropriate local coordinates for the paint.
379 SkMatrix localMatrix;
380 if (!viewMatrix.invert(&localMatrix)) {
381 return;
Michael Ludwig3d6390e2018-10-09 11:45:16 -0400382 }
Michael Ludwig61328202019-06-19 14:48:58 +0000383 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), r,
384 localMatrix);
bsalomoncb31e512016-08-26 10:48:19 -0700385 }
robertphillipsea461502015-05-26 11:38:03 -0700386}
387
Michael Ludwig61328202019-06-19 14:48:58 +0000388enum class GrRenderTargetContext::QuadOptimization {
389 // The rect to draw doesn't intersect clip or render target, so no draw op should be added
390 kDiscarded,
391 // The rect to draw was converted to some other op and appended to the oplist, so no additional
392 // op is necessary. Currently this can convert it to a clear op or a rrect op. Only valid if
393 // a constColor is provided.
394 kSubmitted,
395 // The clip was folded into the device quad, with updated edge flags and local coords, and
396 // caller is responsible for adding an appropriate op.
397 kClipApplied,
398 // No change to clip, but quad updated to better fit clip/render target, and caller is
399 // responsible for adding an appropriate op.
400 kCropped
401};
Michael Ludwig61a16512019-01-15 11:15:13 -0500402
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400403static bool make_vertex_finite(float* value) {
404 if (SkScalarIsNaN(*value)) {
405 return false;
406 }
407
408 if (!SkScalarIsFinite(*value)) {
409 // +/- infinity at this point. Don't use exactly SK_ScalarMax so that we have some precision
410 // left when calculating crops.
411 static constexpr float kNearInfinity = SK_ScalarMax / 4.f;
412 *value = *value < 0.f ? -kNearInfinity : kNearInfinity;
413 }
414
415 return true;
416}
417
Michael Ludwig61328202019-06-19 14:48:58 +0000418GrRenderTargetContext::QuadOptimization GrRenderTargetContext::attemptQuadOptimization(
Michael Ludwige08b4432019-06-19 18:00:48 -0400419 const GrClip& clip, const SkPMColor4f* constColor,
420 const GrUserStencilSettings* stencilSettings, GrAA* aa, GrQuadAAFlags* edgeFlags,
421 GrQuad* deviceQuad, GrQuad* localQuad) {
Michael Ludwig61328202019-06-19 14:48:58 +0000422 // Optimization requirements:
423 // 1. kDiscard applies when clip bounds and quad bounds do not intersect
424 // 2. kClear applies when constColor and final geom is pixel aligned rect;
425 // pixel aligned rect requires rect clip and (rect quad or quad covers clip)
426 // 3. kRRect applies when constColor and rrect clip and quad covers clip
427 // 4. kExplicitClip applies when rect clip and (rect quad or quad covers clip)
428 // 5. kCropped applies when rect quad (currently)
429 // 6. kNone always applies
430 GrQuadAAFlags newFlags = *edgeFlags;
Brian Salomon7694b902019-06-18 21:00:21 +0000431
Michael Ludwige08b4432019-06-19 18:00:48 -0400432 SkRect rtRect;
433 if (stencilSettings) {
Robert Phillipse9462dd2019-10-23 12:41:29 -0400434 // Must use size at which the rendertarget will ultimately be allocated so that stencil
435 // buffer updates on approximately sized render targets don't get corrupted.
436 rtRect = fRenderTargetProxy->backingStoreBoundsRect();
Michael Ludwige08b4432019-06-19 18:00:48 -0400437 } else {
438 // Use the logical size of the render target, which allows for "fullscreen" clears even if
439 // the render target has an approximate backing fit
440 rtRect = SkRect::MakeWH(this->width(), this->height());
441 }
442
Michael Ludwig61328202019-06-19 14:48:58 +0000443 SkRect drawBounds = deviceQuad->bounds();
444 if (constColor) {
445 // Don't bother updating local coordinates when the paint will ignore them anyways
446 localQuad = nullptr;
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400447 // If the device quad is not finite, coerce into a finite quad. This is acceptable since it
448 // will be cropped to the finite 'clip' or render target and there is no local space mapping
449 if (!deviceQuad->isFinite()) {
450 for (int i = 0; i < 4; ++i) {
451 if (!make_vertex_finite(deviceQuad->xs() + i) ||
452 !make_vertex_finite(deviceQuad->ys() + i) ||
453 !make_vertex_finite(deviceQuad->ws() + i)) {
454 // Discard if we see a nan
455 return QuadOptimization::kDiscarded;
456 }
457 }
458 SkASSERT(deviceQuad->isFinite());
459 }
460 } else {
461 // CropToRect requires the quads to be finite. If they are not finite and we have local
462 // coordinates, the mapping from local space to device space is poorly defined so drop it
463 if (!deviceQuad->isFinite()) {
464 return QuadOptimization::kDiscarded;
465 }
Brian Salomon7694b902019-06-18 21:00:21 +0000466 }
467
Michael Ludwig61328202019-06-19 14:48:58 +0000468 // If the quad is entirely off screen, it doesn't matter what the clip does
469 if (!rtRect.intersects(drawBounds)) {
470 return QuadOptimization::kDiscarded;
471 }
Brian Salomon7694b902019-06-18 21:00:21 +0000472
Michael Ludwig61328202019-06-19 14:48:58 +0000473 // Check if clip can be represented as a rounded rect (initialize as if clip fully contained
474 // the render target).
475 SkRRect clipRRect = SkRRect::MakeRect(rtRect);
Michael Ludwige08b4432019-06-19 18:00:48 -0400476 // We initialize clipAA to *aa when there are stencil settings so that we don't artificially
477 // encounter mixed-aa edges (not allowed for stencil), but we want to start as non-AA for
478 // regular draws so that if we fully cover the render target, that can stop being anti-aliased.
479 GrAA clipAA = stencilSettings ? *aa : GrAA::kNo;
Michael Ludwig61328202019-06-19 14:48:58 +0000480 bool axisAlignedClip = true;
Brian Salomon7694b902019-06-18 21:00:21 +0000481 if (!clip.quickContains(rtRect)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000482 if (!clip.isRRect(rtRect, &clipRRect, &clipAA)) {
483 axisAlignedClip = false;
Brian Salomon7694b902019-06-18 21:00:21 +0000484 }
Brian Salomon7694b902019-06-18 21:00:21 +0000485 }
486
Michael Ludwig61328202019-06-19 14:48:58 +0000487 // If the clip rrect is valid (i.e. axis-aligned), we can potentially combine it with the
488 // draw geometry so that no clip is needed when drawing.
Michael Ludwige08b4432019-06-19 18:00:48 -0400489 if (axisAlignedClip && (!stencilSettings || clipAA == *aa)) {
Michael Ludwig61328202019-06-19 14:48:58 +0000490 // Tighten clip bounds (if clipRRect.isRect() is true, clipBounds now holds the intersection
491 // of the render target and the clip rect)
492 SkRect clipBounds = rtRect;
493 if (!clipBounds.intersect(clipRRect.rect()) || !clipBounds.intersects(drawBounds)) {
494 return QuadOptimization::kDiscarded;
Brian Salomon7694b902019-06-18 21:00:21 +0000495 }
496
Michael Ludwig61328202019-06-19 14:48:58 +0000497 if (clipRRect.isRect()) {
498 // No rounded corners, so the kClear and kExplicitClip optimizations are possible
499 if (GrQuadUtils::CropToRect(clipBounds, clipAA, &newFlags, deviceQuad, localQuad)) {
500 if (constColor && deviceQuad->quadType() == GrQuad::Type::kAxisAligned) {
501 // Clear optimization is possible
502 drawBounds = deviceQuad->bounds();
503 if (drawBounds.contains(rtRect)) {
504 // Fullscreen clear
505 this->clear(nullptr, *constColor, CanClearFullscreen::kYes);
506 return QuadOptimization::kSubmitted;
507 } else if (GrClip::IsPixelAligned(drawBounds) &&
508 drawBounds.width() > 256 && drawBounds.height() > 256) {
509 // Scissor + clear (round shouldn't do anything since we are pixel aligned)
510 SkIRect scissorRect;
511 drawBounds.round(&scissorRect);
512 this->clear(&scissorRect, *constColor, CanClearFullscreen::kNo);
513 return QuadOptimization::kSubmitted;
514 }
515 }
516
517 // Update overall AA setting.
518 *edgeFlags = newFlags;
519 if (*aa == GrAA::kNo && clipAA == GrAA::kYes &&
520 newFlags != GrQuadAAFlags::kNone) {
521 // The clip was anti-aliased and now the draw needs to be upgraded to AA to
522 // properly reflect the smooth edge of the clip.
523 *aa = GrAA::kYes;
524 }
525 // We intentionally do not downgrade AA here because we don't know if we need to
526 // preserve MSAA (see GrQuadAAFlags docs). But later in the pipeline, the ops can
527 // use GrResolveAATypeForQuad() to turn off coverage AA when all flags are off.
528
529 // deviceQuad is exactly the intersection of original quad and clip, so it can be
530 // drawn with no clip (submitted by caller)
531 return QuadOptimization::kClipApplied;
532 } else {
533 // The quads have been updated to better fit the clip bounds, but can't get rid of
534 // the clip entirely
535 return QuadOptimization::kCropped;
536 }
537 } else if (constColor) {
538 // Rounded corners and constant filled color (limit ourselves to solid colors because
539 // there is no way to use custom local coordinates with drawRRect).
540 if (GrQuadUtils::CropToRect(clipBounds, clipAA, &newFlags, deviceQuad, localQuad) &&
541 deviceQuad->quadType() == GrQuad::Type::kAxisAligned &&
542 deviceQuad->bounds().contains(clipBounds)) {
543 // Since the cropped quad became a rectangle which covered the bounds of the rrect,
544 // we can draw the rrect directly and ignore the edge flags
545 GrPaint paint;
546 clear_to_grpaint(*constColor, &paint);
547 this->drawRRect(GrFixedClip::Disabled(), std::move(paint), clipAA, SkMatrix::I(),
548 clipRRect, GrStyle::SimpleFill());
549 return QuadOptimization::kSubmitted;
550 } else {
551 // The quad has been updated to better fit clip bounds, but can't remove the clip
552 return QuadOptimization::kCropped;
553 }
Brian Salomon7694b902019-06-18 21:00:21 +0000554 }
Brian Salomon7694b902019-06-18 21:00:21 +0000555 }
556
Michael Ludwig61328202019-06-19 14:48:58 +0000557 // Crop the quad to the conservative bounds of the clip.
558 SkIRect clipDevBounds;
559 clip.getConservativeBounds(rtRect.width(), rtRect.height(), &clipDevBounds);
560 SkRect clipBounds = SkRect::Make(clipDevBounds);
561
562 // One final check for discarding, since we may have gone here directly due to a complex clip
563 if (!clipBounds.intersects(drawBounds)) {
564 return QuadOptimization::kDiscarded;
Brian Salomon7694b902019-06-18 21:00:21 +0000565 }
566
Michael Ludwig61328202019-06-19 14:48:58 +0000567 // Even if this were to return true, the crop rect does not exactly match the clip, so can not
568 // report explicit-clip. Since these edges aren't visible, don't update the final edge flags.
569 GrQuadUtils::CropToRect(clipBounds, clipAA, &newFlags, deviceQuad, localQuad);
570
571 return QuadOptimization::kCropped;
Brian Salomon7694b902019-06-18 21:00:21 +0000572}
573
Michael Ludwig61328202019-06-19 14:48:58 +0000574void GrRenderTargetContext::drawFilledQuad(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500575 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500576 GrAA aa,
Michael Ludwig61328202019-06-19 14:48:58 +0000577 GrQuadAAFlags edgeFlags,
578 const GrQuad& deviceQuad,
579 const GrQuad& localQuad,
Brian Osman11052242016-10-27 14:47:55 -0400580 const GrUserStencilSettings* ss) {
Michael Ludwig61328202019-06-19 14:48:58 +0000581 ASSERT_SINGLE_OWNER
582 RETURN_IF_ABANDONED
583 SkDEBUGCODE(this->validate();)
584 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFilledQuad", fContext);
Michael Ludwig61a16512019-01-15 11:15:13 -0500585
Michael Ludwig61328202019-06-19 14:48:58 +0000586 AutoCheckFlush acf(this->drawingManager());
587
588 SkPMColor4f* constColor = nullptr;
589 SkPMColor4f paintColor;
590 if (!ss && !paint.numCoverageFragmentProcessors() &&
591 paint.isConstantBlendedColor(&paintColor)) {
592 // Only consider clears/rrects when it's easy to guarantee 100% fill with single color
593 constColor = &paintColor;
Michael Ludwig61a16512019-01-15 11:15:13 -0500594 }
595
Michael Ludwig61328202019-06-19 14:48:58 +0000596 GrQuad croppedDeviceQuad = deviceQuad;
597 GrQuad croppedLocalQuad = localQuad;
Michael Ludwige08b4432019-06-19 18:00:48 -0400598 QuadOptimization opt = this->attemptQuadOptimization(clip, constColor, ss, &aa, &edgeFlags,
599 &croppedDeviceQuad, &croppedLocalQuad);
Michael Ludwig61328202019-06-19 14:48:58 +0000600 if (opt >= QuadOptimization::kClipApplied) {
601 // These optimizations require caller to add an op themselves
602 const GrClip& finalClip = opt == QuadOptimization::kClipApplied ? GrFixedClip::Disabled()
603 : clip;
604 GrAAType aaType = ss ? (aa == GrAA::kYes ? GrAAType::kMSAA : GrAAType::kNone)
605 : this->chooseAAType(aa);
606 this->addDrawOp(finalClip, GrFillRectOp::Make(fContext, std::move(paint), aaType, edgeFlags,
607 croppedDeviceQuad, croppedLocalQuad, ss));
csmartdalton97f6cd52016-07-13 13:37:08 -0700608 }
Michael Ludwig61328202019-06-19 14:48:58 +0000609 // All other optimization levels were completely handled inside attempt(), so no extra op needed
robertphillips391395d2016-03-02 09:26:36 -0800610}
611
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000612void GrRenderTargetContext::drawTexturedQuad(const GrClip& clip,
Greg Daniel549325c2019-10-30 16:19:20 -0400613 GrSurfaceProxyView proxyView,
Brian Salomonfc118442019-11-22 19:09:27 -0500614 SkAlphaType srcAlphaType,
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000615 sk_sp<GrColorSpaceXform> textureXform,
616 GrSamplerState::Filter filter,
617 const SkPMColor4f& color,
618 SkBlendMode blendMode,
619 GrAA aa,
620 GrQuadAAFlags edgeFlags,
621 const GrQuad& deviceQuad,
622 const GrQuad& localQuad,
623 const SkRect* domain) {
624 ASSERT_SINGLE_OWNER
625 RETURN_IF_ABANDONED
626 SkDEBUGCODE(this->validate();)
Greg Daniel549325c2019-10-30 16:19:20 -0400627 SkASSERT(proxyView.asTextureProxy());
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000628 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTexturedQuad", fContext);
629
630 AutoCheckFlush acf(this->drawingManager());
631
632 // Functionally this is very similar to drawFilledQuad except that there's no constColor to
633 // enable the kSubmitted optimizations, no stencil settings support, and its a GrTextureOp.
634 GrQuad croppedDeviceQuad = deviceQuad;
635 GrQuad croppedLocalQuad = localQuad;
636 QuadOptimization opt = this->attemptQuadOptimization(clip, nullptr, nullptr, &aa, &edgeFlags,
637 &croppedDeviceQuad, &croppedLocalQuad);
638
639 SkASSERT(opt != QuadOptimization::kSubmitted);
640 if (opt != QuadOptimization::kDiscarded) {
641 // And the texture op if not discarded
642 const GrClip& finalClip = opt == QuadOptimization::kClipApplied ? GrFixedClip::Disabled()
643 : clip;
644 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400645 auto clampType = GrColorTypeClampType(this->colorInfo().colorType());
Brian Salomonf19f9ca2019-09-18 15:54:26 -0400646 auto saturate = clampType == GrClampType::kManual ? GrTextureOp::Saturate::kYes
647 : GrTextureOp::Saturate::kNo;
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000648 // Use the provided domain, although hypothetically we could detect that the cropped local
649 // quad is sufficiently inside the domain and the constraint could be dropped.
Brian Salomonfc118442019-11-22 19:09:27 -0500650 this->addDrawOp(
651 finalClip,
652 GrTextureOp::Make(fContext, std::move(proxyView), srcAlphaType,
653 std::move(textureXform), filter, color, saturate, blendMode,
654 aaType, edgeFlags, croppedDeviceQuad, croppedLocalQuad, domain));
Michael Ludwigaee26ea2019-07-08 16:22:48 +0000655 }
656}
657
Brian Osman11052242016-10-27 14:47:55 -0400658void GrRenderTargetContext::drawRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500659 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500660 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400661 const SkMatrix& viewMatrix,
662 const SkRect& rect,
663 const GrStyle* style) {
bsalomon6663acf2016-05-10 09:14:17 -0700664 if (!style) {
665 style = &GrStyle::SimpleFill();
666 }
joshualitt1de610a2016-01-06 08:26:09 -0800667 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700668 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700669 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400670 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRect", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -0700671
bsalomon6663acf2016-05-10 09:14:17 -0700672 // Path effects should've been devolved to a path in SkGpuDevice
673 SkASSERT(!style->pathEffect());
robertphillipsea461502015-05-26 11:38:03 -0700674
Robert Phillips72152832017-01-25 17:31:35 -0500675 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -0700676
bsalomon6663acf2016-05-10 09:14:17 -0700677 const SkStrokeRec& stroke = style->strokeRec();
Robert Phillips8c8b0462018-08-24 16:18:03 -0400678 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
Michael Ludwig61328202019-06-19 14:48:58 +0000679 // Fills the rect, using rect as its own local coordinates
680 this->fillRectToRect(clip, std::move(paint), aa, viewMatrix, rect, rect);
Michael Ludwig61a16512019-01-15 11:15:13 -0500681 return;
bsalomona7d85ba2016-07-06 11:54:59 -0700682 } else if (stroke.getStyle() == SkStrokeRec::kStroke_Style ||
683 stroke.getStyle() == SkStrokeRec::kHairline_Style) {
684 if ((!rect.width() || !rect.height()) &&
685 SkStrokeRec::kHairline_Style != stroke.getStyle()) {
686 SkScalar r = stroke.getWidth() / 2;
687 // TODO: Move these stroke->fill fallbacks to GrShape?
688 switch (stroke.getJoin()) {
689 case SkPaint::kMiter_Join:
Brian Salomon82f44312017-01-11 13:42:54 -0500690 this->drawRect(
691 clip, std::move(paint), aa, viewMatrix,
692 {rect.fLeft - r, rect.fTop - r, rect.fRight + r, rect.fBottom + r},
693 &GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700694 return;
695 case SkPaint::kRound_Join:
696 // Raster draws nothing when both dimensions are empty.
697 if (rect.width() || rect.height()){
698 SkRRect rrect = SkRRect::MakeRectXY(rect.makeOutset(r, r), r, r);
Brian Salomon82f44312017-01-11 13:42:54 -0500699 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect,
700 GrStyle::SimpleFill());
bsalomona7d85ba2016-07-06 11:54:59 -0700701 return;
702 }
703 case SkPaint::kBevel_Join:
704 if (!rect.width()) {
Brian Salomon82f44312017-01-11 13:42:54 -0500705 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700706 {rect.fLeft - r, rect.fTop, rect.fRight + r, rect.fBottom},
707 &GrStyle::SimpleFill());
708 } else {
Brian Salomon82f44312017-01-11 13:42:54 -0500709 this->drawRect(clip, std::move(paint), aa, viewMatrix,
bsalomona7d85ba2016-07-06 11:54:59 -0700710 {rect.fLeft, rect.fTop - r, rect.fRight, rect.fBottom + r},
711 &GrStyle::SimpleFill());
712 }
713 return;
714 }
715 }
robertphillips44302392016-07-08 14:43:03 -0700716
Brian Salomonbaaf4392017-06-15 09:59:23 -0400717 std::unique_ptr<GrDrawOp> op;
robertphillips44302392016-07-08 14:43:03 -0700718
Chris Dalton7d6748e2019-03-13 00:34:52 -0600719 GrAAType aaType = this->chooseAAType(aa);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500720 op = GrStrokeRectOp::Make(fContext, std::move(paint), aaType, viewMatrix, rect, stroke);
721 // op may be null if the stroke is not supported or if using coverage aa and the view matrix
722 // does not preserve rectangles.
Brian Salomon42521e82016-12-07 16:44:58 -0500723 if (op) {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400724 this->addDrawOp(clip, std::move(op));
robertphillips44302392016-07-08 14:43:03 -0700725 return;
robertphillips4bc31812016-03-01 12:22:49 -0800726 }
robertphillips4bc31812016-03-01 12:22:49 -0800727 }
Mike Klein16885072018-12-11 09:54:31 -0500728 assert_alive(paint);
Brian Salomon2fad74a2017-12-20 13:28:55 -0500729 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrShape(rect, *style));
robertphillipsea461502015-05-26 11:38:03 -0700730}
731
Michael Ludwig69858532018-11-28 15:34:34 -0500732void GrRenderTargetContext::drawQuadSet(const GrClip& clip, GrPaint&& paint, GrAA aa,
733 const SkMatrix& viewMatrix, const QuadSetEntry quads[],
734 int cnt) {
Chris Dalton7d6748e2019-03-13 00:34:52 -0600735 GrAAType aaType = this->chooseAAType(aa);
Robert Phillips438d9862019-11-14 12:46:05 -0500736
737 GrFillRectOp::AddFillRectOps(this, clip, fContext, std::move(paint), aaType, viewMatrix,
738 quads, cnt);
Michael Ludwig69858532018-11-28 15:34:34 -0500739}
740
Robert Phillipsec2249f2016-11-09 08:54:35 -0500741int GrRenderTargetContextPriv::maxWindowRectangles() const {
742 return fRenderTargetContext->fRenderTargetProxy->maxWindowRectangles(
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400743 *fRenderTargetContext->caps());
Robert Phillipsec2249f2016-11-09 08:54:35 -0500744}
745
Greg Danielf41b2bd2019-08-22 16:19:24 -0400746GrOpsTask::CanDiscardPreviousOps GrRenderTargetContext::canDiscardPreviousOpsOnFullClear(
Chris Dalton6b982802019-06-27 13:53:46 -0600747 ) const {
748#if GR_TEST_UTILS
749 if (fPreserveOpsOnFullClear_TestingOnly) {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400750 return GrOpsTask::CanDiscardPreviousOps::kNo;
Chris Dalton6b982802019-06-27 13:53:46 -0600751 }
752#endif
753 // Regardless of how the clear is implemented (native clear or a fullscreen quad), all prior ops
754 // would normally be overwritten. The one exception is if the render target context is marked as
755 // needing a stencil buffer then there may be a prior op that writes to the stencil buffer.
756 // Although the clear will ignore the stencil buffer, following draw ops may not so we can't get
757 // rid of all the preceding ops. Beware! If we ever add any ops that have a side effect beyond
758 // modifying the stencil buffer we will need a more elaborate tracking system (skbug.com/7002).
Greg Danielf41b2bd2019-08-22 16:19:24 -0400759 return GrOpsTask::CanDiscardPreviousOps(!fNumStencilSamples);
Chris Dalton6b982802019-06-27 13:53:46 -0600760}
761
Chris Dalton858cf232019-10-14 16:20:00 -0600762void GrRenderTargetContext::setNeedsStencil(bool useMixedSamplesIfNotMSAA) {
Chris Daltoneffee202019-07-01 22:28:03 -0600763 // Don't clear stencil until after we've changed fNumStencilSamples. This ensures we don't loop
Chris Dalton6b982802019-06-27 13:53:46 -0600764 // forever in the event that there are driver bugs and we need to clear as a draw.
Chris Dalton674f77a2019-09-30 20:49:39 -0600765 bool hasInitializedStencil = fNumStencilSamples > 0;
Chris Dalton6b982802019-06-27 13:53:46 -0600766
Chris Daltoneffee202019-07-01 22:28:03 -0600767 int numRequiredSamples = this->numSamples();
Chris Dalton858cf232019-10-14 16:20:00 -0600768 if (useMixedSamplesIfNotMSAA && 1 == numRequiredSamples) {
Chris Daltoneffee202019-07-01 22:28:03 -0600769 SkASSERT(fRenderTargetProxy->canUseMixedSamples(*this->caps()));
770 numRequiredSamples = this->caps()->internalMultisampleCount(
Greg Danieleadfac92019-08-02 09:03:53 -0400771 this->asSurfaceProxy()->backendFormat());
Chris Daltoneffee202019-07-01 22:28:03 -0600772 }
773 SkASSERT(numRequiredSamples > 0);
774
775 if (numRequiredSamples > fNumStencilSamples) {
776 fNumStencilSamples = numRequiredSamples;
777 fRenderTargetProxy->setNeedsStencil(fNumStencilSamples);
778 }
Chris Dalton6b982802019-06-27 13:53:46 -0600779
Chris Dalton674f77a2019-09-30 20:49:39 -0600780 if (!hasInitializedStencil) {
Chris Dalton6b982802019-06-27 13:53:46 -0600781 if (this->caps()->performStencilClearsAsDraws()) {
782 // There is a driver bug with clearing stencil. We must use an op to manually clear the
783 // stencil buffer before the op that required 'setNeedsStencil'.
784 this->internalStencilClear(GrFixedClip::Disabled(), /* inside mask */ false);
785 } else {
Chris Dalton674f77a2019-09-30 20:49:39 -0600786 this->getOpsTask()->setInitialStencilContent(
787 GrOpsTask::StencilContent::kUserBitsCleared);
Chris Dalton6b982802019-06-27 13:53:46 -0600788 }
789 }
790}
791
Jim Van Verth6a40abc2017-11-02 16:56:09 +0000792void GrRenderTargetContextPriv::clearStencilClip(const GrFixedClip& clip, bool insideStencilMask) {
robertphillips976f5f02016-06-03 10:59:20 -0700793 ASSERT_SINGLE_OWNER_PRIV
794 RETURN_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -0400795 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400796 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "clearStencilClip",
797 fRenderTargetContext->fContext);
robertphillips976f5f02016-06-03 10:59:20 -0700798
Robert Phillips72152832017-01-25 17:31:35 -0500799 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400800
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500801 fRenderTargetContext->internalStencilClear(clip, insideStencilMask);
802}
803
804void GrRenderTargetContext::internalStencilClear(const GrFixedClip& clip, bool insideStencilMask) {
Chris Dalton858cf232019-10-14 16:20:00 -0600805 this->setNeedsStencil(/* useMixedSamplesIfNotMSAA = */ false);
806
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500807 if (this->caps()->performStencilClearsAsDraws()) {
808 const GrUserStencilSettings* ss = GrStencilSettings::SetClipBitSettings(insideStencilMask);
809 SkRect rtRect = SkRect::MakeWH(this->width(), this->height());
810
811 // Configure the paint to have no impact on the color buffer
812 GrPaint paint;
Michael Ludwig0cb2fde2019-05-28 13:14:41 -0400813 paint.setXPFactory(GrDisableColorXPFactory::Get());
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400814 this->addDrawOp(clip, GrFillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
815 rtRect, ss));
Michael Ludwigc39d0c82019-01-15 10:03:43 -0500816 } else {
817 std::unique_ptr<GrOp> op(GrClearStencilClipOp::Make(fContext, clip, insideStencilMask,
818 fRenderTargetProxy.get()));
819 if (!op) {
820 return;
821 }
Chris Dalton08755122019-08-05 16:13:47 -0600822 this->addOp(std::move(op));
Robert Phillipse60ad622016-11-17 10:22:48 -0500823 }
robertphillips976f5f02016-06-03 10:59:20 -0700824}
825
Chris Daltonbbfd5162017-11-07 13:35:22 -0700826void GrRenderTargetContextPriv::stencilPath(const GrHardClip& clip,
Chris Dalton09e56892019-03-13 00:22:01 -0600827 GrAA doStencilMSAA,
Brian Osman11052242016-10-27 14:47:55 -0400828 const SkMatrix& viewMatrix,
Robert Phillipse1efd382019-08-21 10:07:10 -0400829 sk_sp<const GrPath> path) {
Brian Salomon467921e2017-03-06 16:17:12 -0500830 ASSERT_SINGLE_OWNER_PRIV
831 RETURN_IF_ABANDONED_PRIV
832 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400833 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "stencilPath",
834 fRenderTargetContext->fContext);
Brian Salomon467921e2017-03-06 16:17:12 -0500835
Brian Salomon467921e2017-03-06 16:17:12 -0500836 // TODO: extract portions of checkDraw that are relevant to path stenciling.
837 SkASSERT(path);
838 SkASSERT(fRenderTargetContext->caps()->shaderCaps()->pathRenderingSupport());
839
840 // FIXME: Use path bounds instead of this WAR once
841 // https://bugs.chromium.org/p/skia/issues/detail?id=5640 is resolved.
842 SkRect bounds = SkRect::MakeIWH(fRenderTargetContext->width(), fRenderTargetContext->height());
843
844 // Setup clip
Chris Daltonbbfd5162017-11-07 13:35:22 -0700845 GrAppliedHardClip appliedClip;
846 if (!clip.apply(fRenderTargetContext->width(), fRenderTargetContext->height(), &appliedClip,
847 &bounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -0500848 return;
849 }
850
Robert Phillips7c525e62018-06-12 10:11:12 -0400851 std::unique_ptr<GrOp> op = GrStencilPathOp::Make(fRenderTargetContext->fContext,
852 viewMatrix,
Chris Dalton09e56892019-03-13 00:22:01 -0600853 GrAA::kYes == doStencilMSAA,
Brian Salomon467921e2017-03-06 16:17:12 -0500854 appliedClip.hasStencilClip(),
Brian Salomon467921e2017-03-06 16:17:12 -0500855 appliedClip.scissorState(),
Robert Phillipse1efd382019-08-21 10:07:10 -0400856 std::move(path));
Robert Phillipsb9a02a12017-04-06 11:08:40 -0400857 if (!op) {
858 return;
859 }
Brian Salomon97180af2017-03-14 13:42:58 -0400860 op->setClippedBounds(bounds);
Chris Dalton6b982802019-06-27 13:53:46 -0600861
Chris Daltoneffee202019-07-01 22:28:03 -0600862 fRenderTargetContext->setNeedsStencil(GrAA::kYes == doStencilMSAA);
Chris Dalton08755122019-08-05 16:13:47 -0600863 fRenderTargetContext->addOp(std::move(op));
robertphillips976f5f02016-06-03 10:59:20 -0700864}
865
Brian Salomond7065e72018-10-12 11:42:02 -0400866void GrRenderTargetContext::drawTextureSet(const GrClip& clip, const TextureSetEntry set[], int cnt,
Michael Ludwiga3c45c72019-01-17 17:26:48 -0500867 GrSamplerState::Filter filter, SkBlendMode mode,
Michael Ludwig31ba7182019-04-03 10:38:06 -0400868 GrAA aa, SkCanvas::SrcRectConstraint constraint,
869 const SkMatrix& viewMatrix,
Brian Osman3d139a42018-11-19 10:42:10 -0500870 sk_sp<GrColorSpaceXform> texXform) {
Brian Salomond7065e72018-10-12 11:42:02 -0400871 ASSERT_SINGLE_OWNER
872 RETURN_IF_ABANDONED
873 SkDEBUGCODE(this->validate();)
874 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawTextureSet", fContext);
Michael Ludwiga3c45c72019-01-17 17:26:48 -0500875
Michael Ludwigfe13ca32019-11-21 10:26:41 -0500876 // Create the minimum number of GrTextureOps needed to draw this set. Individual
877 // GrTextureOps can rebind the texture between draws thus avoiding GrPaint (re)creation.
878 AutoCheckFlush acf(this->drawingManager());
879 GrAAType aaType = this->chooseAAType(aa);
880 auto clampType = GrColorTypeClampType(this->colorInfo().colorType());
881 auto saturate = clampType == GrClampType::kManual ? GrTextureOp::Saturate::kYes
882 : GrTextureOp::Saturate::kNo;
883 GrTextureOp::AddTextureSetOps(this, clip, fContext, set, cnt, filter, saturate, mode, aaType,
884 constraint, viewMatrix, std::move(texXform));
Brian Salomond7065e72018-10-12 11:42:02 -0400885}
886
Brian Osman11052242016-10-27 14:47:55 -0400887void GrRenderTargetContext::drawVertices(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500888 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -0400889 const SkMatrix& viewMatrix,
Brian Osmanae0c50c2017-05-25 16:56:34 -0400890 sk_sp<SkVertices> vertices,
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400891 const SkVertices::Bone bones[],
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400892 int boneCount,
Brian Osmanae0c50c2017-05-25 16:56:34 -0400893 GrPrimitiveType* overridePrimType) {
Brian Salomon199fb872017-02-06 09:41:10 -0500894 ASSERT_SINGLE_OWNER
895 RETURN_IF_ABANDONED
896 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400897 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawVertices", fContext);
Brian Salomon199fb872017-02-06 09:41:10 -0500898
899 AutoCheckFlush acf(this->drawingManager());
900
901 SkASSERT(vertices);
Chris Dalton7d6748e2019-03-13 00:34:52 -0600902 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Brian Salomonf3569f02017-10-24 12:52:33 -0400903 std::unique_ptr<GrDrawOp> op = GrDrawVerticesOp::Make(
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400904 fContext, std::move(paint), std::move(vertices), bones, boneCount, viewMatrix, aaType,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400905 this->colorInfo().refColorSpaceXformFromSRGB(), overridePrimType);
Brian Salomonc2f42542017-07-12 14:11:22 -0400906 this->addDrawOp(clip, std::move(op));
robertphillipsea461502015-05-26 11:38:03 -0700907}
908
909///////////////////////////////////////////////////////////////////////////////
910
Brian Osman4d92b892019-03-24 00:53:23 +0000911void GrRenderTargetContext::drawAtlas(const GrClip& clip,
912 GrPaint&& paint,
913 const SkMatrix& viewMatrix,
914 int spriteCount,
915 const SkRSXform xform[],
916 const SkRect texRect[],
917 const SkColor colors[]) {
918 ASSERT_SINGLE_OWNER
919 RETURN_IF_ABANDONED
920 SkDEBUGCODE(this->validate();)
921 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawAtlas", fContext);
922
923 AutoCheckFlush acf(this->drawingManager());
924
925 GrAAType aaType = this->chooseAAType(GrAA::kNo);
926 std::unique_ptr<GrDrawOp> op = GrDrawAtlasOp::Make(fContext, std::move(paint), viewMatrix,
927 aaType, spriteCount, xform, texRect, colors);
928 this->addDrawOp(clip, std::move(op));
929}
930
931///////////////////////////////////////////////////////////////////////////////
932
Brian Osman11052242016-10-27 14:47:55 -0400933void GrRenderTargetContext::drawRRect(const GrClip& origClip,
Brian Salomon82f44312017-01-11 13:42:54 -0500934 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500935 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -0400936 const SkMatrix& viewMatrix,
937 const SkRRect& rrect,
938 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -0800939 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700940 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -0700941 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -0400942 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRRect", fContext);
Robert Phillips85290802018-07-02 13:14:28 -0400943
944 const SkStrokeRec& stroke = style.strokeRec();
945 if (stroke.getStyle() == SkStrokeRec::kFill_Style && rrect.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -0700946 return;
947 }
948
bsalomon7f0d9f32016-08-15 14:49:10 -0700949 GrNoClip noclip;
950 const GrClip* clip = &origClip;
951#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
952 // The Android framework frequently clips rrects to themselves where the clip is non-aa and the
Brian Salomon42521e82016-12-07 16:44:58 -0500953 // draw is aa. Since our lower level clip code works from op bounds, which are SkRects, it
bsalomon7f0d9f32016-08-15 14:49:10 -0700954 // doesn't detect that the clip can be ignored (modulo antialiasing). The following test
955 // attempts to mitigate the stencil clip cost but will only help when the entire clip stack
Michael Ludwig28398842019-03-25 10:24:24 -0400956 // can be ignored. We'd prefer to fix this in the framework by removing the clips calls. This
957 // only works for filled rrects since the stroke width outsets beyond the rrect itself.
bsalomon7f0d9f32016-08-15 14:49:10 -0700958 SkRRect devRRect;
Michael Ludwig28398842019-03-25 10:24:24 -0400959 if (stroke.getStyle() == SkStrokeRec::kFill_Style && rrect.transform(viewMatrix, &devRRect) &&
960 clip->quickContains(devRRect)) {
bsalomon7f0d9f32016-08-15 14:49:10 -0700961 clip = &noclip;
962 }
963#endif
bsalomon6663acf2016-05-10 09:14:17 -0700964 SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
ksakamotoec7f2ac2016-07-05 03:54:53 -0700965
Robert Phillips72152832017-01-25 17:31:35 -0500966 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -0700967
Chris Dalton7d6748e2019-03-13 00:34:52 -0600968 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton133944a2018-11-16 23:30:29 -0500969
Chris Dalton0dffbab2019-03-27 13:08:50 -0600970 std::unique_ptr<GrDrawOp> op;
Jim Van Verth64b85892019-06-17 12:01:46 -0400971 if (GrAAType::kCoverage == aaType && rrect.isSimple() &&
972 rrect.getSimpleRadii().fX == rrect.getSimpleRadii().fY &&
973 viewMatrix.rectStaysRect() && viewMatrix.isSimilarity()) {
974 // In coverage mode, we draw axis-aligned circular roundrects with the GrOvalOpFactory
975 // to avoid perf regressions on some platforms.
976 assert_alive(paint);
977 op = GrOvalOpFactory::MakeCircularRRectOp(
978 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
979 }
980 if (!op && style.isSimpleFill()) {
Chris Dalton0dffbab2019-03-27 13:08:50 -0600981 assert_alive(paint);
982 op = GrFillRRectOp::Make(
983 fContext, aaType, viewMatrix, rrect, *this->caps(), std::move(paint));
984 }
Greg Daniel2655ede2019-04-10 00:49:28 +0000985 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -0600986 assert_alive(paint);
987 op = GrOvalOpFactory::MakeRRectOp(
Greg Daniel2655ede2019-04-10 00:49:28 +0000988 fContext, std::move(paint), viewMatrix, rrect, stroke, this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -0600989 }
990 if (op) {
991 this->addDrawOp(*clip, std::move(op));
992 return;
robertphillipsea461502015-05-26 11:38:03 -0700993 }
robertphillipsb56f9272016-02-25 11:03:52 -0800994
Mike Klein16885072018-12-11 09:54:31 -0500995 assert_alive(paint);
Brian Salomon2fad74a2017-12-20 13:28:55 -0500996 this->drawShapeUsingPathRenderer(*clip, std::move(paint), aa, viewMatrix,
997 GrShape(rrect, style));
robertphillipsea461502015-05-26 11:38:03 -0700998}
999
Jim Van Verthc5903412016-11-17 15:27:09 -05001000///////////////////////////////////////////////////////////////////////////////
1001
Jim Van Verth3af1af92017-05-18 15:06:54 -04001002static SkPoint3 map(const SkMatrix& m, const SkPoint3& pt) {
1003 SkPoint3 result;
1004 m.mapXY(pt.fX, pt.fY, (SkPoint*)&result.fX);
1005 result.fZ = pt.fZ;
1006 return result;
1007}
1008
1009bool GrRenderTargetContext::drawFastShadow(const GrClip& clip,
Jim Van Verth3af1af92017-05-18 15:06:54 -04001010 const SkMatrix& viewMatrix,
1011 const SkPath& path,
1012 const SkDrawShadowRec& rec) {
Jim Van Verthc5903412016-11-17 15:27:09 -05001013 ASSERT_SINGLE_OWNER
Robert Phillips6a6de562019-02-15 15:19:15 -05001014 if (fContext->priv().abandoned()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001015 return true;
1016 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001017 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001018 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawFastShadow", fContext);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001019
1020 // check z plane
1021 bool tiltZPlane = SkToBool(!SkScalarNearlyZero(rec.fZPlaneParams.fX) ||
1022 !SkScalarNearlyZero(rec.fZPlaneParams.fY));
1023 bool skipAnalytic = SkToBool(rec.fFlags & SkShadowFlags::kGeometricOnly_ShadowFlag);
1024 if (tiltZPlane || skipAnalytic || !viewMatrix.rectStaysRect() || !viewMatrix.isSimilarity()) {
1025 return false;
1026 }
1027
1028 SkRRect rrect;
1029 SkRect rect;
1030 // we can only handle rects, circles, and rrects with circular corners
Mike Reed242135a2018-02-22 13:41:39 -05001031 bool isRRect = path.isRRect(&rrect) && SkRRectPriv::IsSimpleCircular(rrect) &&
Jim Van Verth3af1af92017-05-18 15:06:54 -04001032 rrect.radii(SkRRect::kUpperLeft_Corner).fX > SK_ScalarNearlyZero;
1033 if (!isRRect &&
1034 path.isOval(&rect) && SkScalarNearlyEqual(rect.width(), rect.height()) &&
1035 rect.width() > SK_ScalarNearlyZero) {
1036 rrect.setOval(rect);
1037 isRRect = true;
1038 }
1039 if (!isRRect && path.isRect(&rect)) {
1040 rrect.setRect(rect);
1041 isRRect = true;
1042 }
1043
1044 if (!isRRect) {
1045 return false;
1046 }
1047
Jim Van Verthc5903412016-11-17 15:27:09 -05001048 if (rrect.isEmpty()) {
Jim Van Verth3af1af92017-05-18 15:06:54 -04001049 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001050 }
1051
Robert Phillips72152832017-01-25 17:31:35 -05001052 AutoCheckFlush acf(this->drawingManager());
Jim Van Verthc5903412016-11-17 15:27:09 -05001053
Jim Van Verth3af1af92017-05-18 15:06:54 -04001054 // transform light
1055 SkPoint3 devLightPos = map(viewMatrix, rec.fLightPos);
1056
1057 // 1/scale
1058 SkScalar devToSrcScale = viewMatrix.isScaleTranslate() ?
Jim Van Vertheda9a552019-07-24 14:46:53 -04001059 SkScalarInvert(SkScalarAbs(viewMatrix[SkMatrix::kMScaleX])) :
Jim Van Verth3af1af92017-05-18 15:06:54 -04001060 sk_float_rsqrt(viewMatrix[SkMatrix::kMScaleX] * viewMatrix[SkMatrix::kMScaleX] +
1061 viewMatrix[SkMatrix::kMSkewX] * viewMatrix[SkMatrix::kMSkewX]);
1062
1063 SkScalar occluderHeight = rec.fZPlaneParams.fZ;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001064 bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
1065
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001066 if (SkColorGetA(rec.fAmbientColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001067 SkScalar devSpaceInsetWidth = SkDrawShadowMetrics::AmbientBlurRadius(occluderHeight);
1068 const SkScalar umbraRecipAlpha = SkDrawShadowMetrics::AmbientRecipAlpha(occluderHeight);
1069 const SkScalar devSpaceAmbientBlur = devSpaceInsetWidth * umbraRecipAlpha;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001070
1071 // Outset the shadow rrect to the border of the penumbra
1072 SkScalar ambientPathOutset = devSpaceInsetWidth * devToSrcScale;
1073 SkRRect ambientRRect;
1074 SkRect outsetRect = rrect.rect().makeOutset(ambientPathOutset, ambientPathOutset);
1075 // If the rrect was an oval then its outset will also be one.
1076 // We set it explicitly to avoid errors.
1077 if (rrect.isOval()) {
1078 ambientRRect = SkRRect::MakeOval(outsetRect);
1079 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001080 SkScalar outsetRad = SkRRectPriv::GetSimpleRadii(rrect).fX + ambientPathOutset;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001081 ambientRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1082 }
1083
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001084 GrColor ambientColor = SkColorToPremulGrColor(rec.fAmbientColor);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001085 if (transparent) {
1086 // set a large inset to force a fill
1087 devSpaceInsetWidth = ambientRRect.width();
1088 }
Jim Van Verth39e71652018-04-23 18:08:45 +00001089
Robert Phillips7c525e62018-06-12 10:11:12 -04001090 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1091 ambientColor,
1092 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001093 ambientRRect,
1094 devSpaceAmbientBlur,
Jim Van Verthfb186392018-09-11 11:37:46 -04001095 devSpaceInsetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001096 if (op) {
1097 this->addDrawOp(clip, std::move(op));
1098 }
Jim Van Verthc5903412016-11-17 15:27:09 -05001099 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001100
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001101 if (SkColorGetA(rec.fSpotColor) > 0) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001102 SkScalar devSpaceSpotBlur;
1103 SkScalar spotScale;
1104 SkVector spotOffset;
1105 SkDrawShadowMetrics::GetSpotParams(occluderHeight, devLightPos.fX, devLightPos.fY,
1106 devLightPos.fZ, rec.fLightRadius,
1107 &devSpaceSpotBlur, &spotScale, &spotOffset);
1108 // handle scale of radius due to CTM
Jim Van Verth3af1af92017-05-18 15:06:54 -04001109 const SkScalar srcSpaceSpotBlur = devSpaceSpotBlur * devToSrcScale;
1110
Jim Van Verth3af1af92017-05-18 15:06:54 -04001111 // Adjust translate for the effect of the scale.
1112 spotOffset.fX += spotScale*viewMatrix[SkMatrix::kMTransX];
1113 spotOffset.fY += spotScale*viewMatrix[SkMatrix::kMTransY];
1114 // This offset is in dev space, need to transform it into source space.
1115 SkMatrix ctmInverse;
1116 if (viewMatrix.invert(&ctmInverse)) {
1117 ctmInverse.mapPoints(&spotOffset, 1);
1118 } else {
1119 // Since the matrix is a similarity, this should never happen, but just in case...
1120 SkDebugf("Matrix is degenerate. Will not render spot shadow correctly!\n");
1121 SkASSERT(false);
1122 }
1123
1124 // Compute the transformed shadow rrect
1125 SkRRect spotShadowRRect;
1126 SkMatrix shadowTransform;
1127 shadowTransform.setScaleTranslate(spotScale, spotScale, spotOffset.fX, spotOffset.fY);
1128 rrect.transform(shadowTransform, &spotShadowRRect);
Mike Reed242135a2018-02-22 13:41:39 -05001129 SkScalar spotRadius = SkRRectPriv::GetSimpleRadii(spotShadowRRect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001130
1131 // Compute the insetWidth
Jim Van Verth1af03d42017-07-31 09:34:58 -04001132 SkScalar blurOutset = srcSpaceSpotBlur;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001133 SkScalar insetWidth = blurOutset;
1134 if (transparent) {
1135 // If transparent, just do a fill
1136 insetWidth += spotShadowRRect.width();
1137 } else {
1138 // For shadows, instead of using a stroke we specify an inset from the penumbra
1139 // border. We want to extend this inset area so that it meets up with the caster
1140 // geometry. The inset geometry will by default already be inset by the blur width.
1141 //
1142 // We compare the min and max corners inset by the radius between the original
1143 // rrect and the shadow rrect. The distance between the two plus the difference
1144 // between the scaled radius and the original radius gives the distance from the
1145 // transformed shadow shape to the original shape in that corner. The max
1146 // of these gives the maximum distance we need to cover.
1147 //
1148 // Since we are outsetting by 1/2 the blur distance, we just add the maxOffset to
1149 // that to get the full insetWidth.
1150 SkScalar maxOffset;
1151 if (rrect.isRect()) {
1152 // Manhattan distance works better for rects
1153 maxOffset = SkTMax(SkTMax(SkTAbs(spotShadowRRect.rect().fLeft -
1154 rrect.rect().fLeft),
1155 SkTAbs(spotShadowRRect.rect().fTop -
1156 rrect.rect().fTop)),
1157 SkTMax(SkTAbs(spotShadowRRect.rect().fRight -
1158 rrect.rect().fRight),
1159 SkTAbs(spotShadowRRect.rect().fBottom -
1160 rrect.rect().fBottom)));
1161 } else {
Mike Reed242135a2018-02-22 13:41:39 -05001162 SkScalar dr = spotRadius - SkRRectPriv::GetSimpleRadii(rrect).fX;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001163 SkPoint upperLeftOffset = SkPoint::Make(spotShadowRRect.rect().fLeft -
1164 rrect.rect().fLeft + dr,
1165 spotShadowRRect.rect().fTop -
1166 rrect.rect().fTop + dr);
1167 SkPoint lowerRightOffset = SkPoint::Make(spotShadowRRect.rect().fRight -
1168 rrect.rect().fRight - dr,
1169 spotShadowRRect.rect().fBottom -
1170 rrect.rect().fBottom - dr);
Cary Clarkdf429f32017-11-08 11:44:31 -05001171 maxOffset = SkScalarSqrt(SkTMax(SkPointPriv::LengthSqd(upperLeftOffset),
1172 SkPointPriv::LengthSqd(lowerRightOffset))) + dr;
Jim Van Verth3af1af92017-05-18 15:06:54 -04001173 }
Jim Van Verth4c8c1e82018-04-23 17:14:48 -04001174 insetWidth += SkTMax(blurOutset, maxOffset);
Jim Van Verth3af1af92017-05-18 15:06:54 -04001175 }
1176
1177 // Outset the shadow rrect to the border of the penumbra
1178 SkRect outsetRect = spotShadowRRect.rect().makeOutset(blurOutset, blurOutset);
1179 if (spotShadowRRect.isOval()) {
1180 spotShadowRRect = SkRRect::MakeOval(outsetRect);
1181 } else {
1182 SkScalar outsetRad = spotRadius + blurOutset;
1183 spotShadowRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
1184 }
1185
Jim Van Verthb1b80f72018-01-18 15:19:13 -05001186 GrColor spotColor = SkColorToPremulGrColor(rec.fSpotColor);
Jim Van Verth34d6e4b2017-06-09 11:09:03 -04001187
Robert Phillips7c525e62018-06-12 10:11:12 -04001188 std::unique_ptr<GrDrawOp> op = GrShadowRRectOp::Make(fContext,
1189 spotColor,
1190 viewMatrix,
Brian Salomon05969092017-07-13 11:20:51 -04001191 spotShadowRRect,
Jim Van Verth1af03d42017-07-31 09:34:58 -04001192 2.0f * devSpaceSpotBlur,
Brian Salomon05969092017-07-13 11:20:51 -04001193 insetWidth);
Robert Phillipse5763782019-04-17 14:38:24 -04001194 if (op) {
1195 this->addDrawOp(clip, std::move(op));
1196 }
Jim Van Verth3af1af92017-05-18 15:06:54 -04001197 }
1198
1199 return true;
Jim Van Verthc5903412016-11-17 15:27:09 -05001200}
1201
1202///////////////////////////////////////////////////////////////////////////////
1203
Brian Osman11052242016-10-27 14:47:55 -04001204bool GrRenderTargetContext::drawFilledDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001205 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001206 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001207 const SkMatrix& viewMatrix,
1208 const SkRRect& origOuter,
1209 const SkRRect& origInner) {
robertphillips00095892016-02-29 13:50:40 -08001210 SkASSERT(!origInner.isEmpty());
1211 SkASSERT(!origOuter.isEmpty());
1212
Brian Salomon65749212017-12-01 16:01:47 -05001213 SkTCopyOnFirstWrite<SkRRect> inner(origInner), outer(origOuter);
1214
Chris Dalton7d6748e2019-03-13 00:34:52 -06001215 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon45839f92017-12-04 09:02:35 -05001216
1217 if (GrAAType::kMSAA == aaType) {
1218 return false;
1219 }
1220
Greg Daniel2655ede2019-04-10 00:49:28 +00001221 if (GrAAType::kCoverage == aaType && SkRRectPriv::IsCircle(*inner)
1222 && SkRRectPriv::IsCircle(*outer)) {
Brian Salomon65749212017-12-01 16:01:47 -05001223 auto outerR = outer->width() / 2.f;
1224 auto innerR = inner->width() / 2.f;
1225 auto cx = outer->getBounds().fLeft + outerR;
1226 auto cy = outer->getBounds().fTop + outerR;
1227 if (SkScalarNearlyEqual(cx, inner->getBounds().fLeft + innerR) &&
1228 SkScalarNearlyEqual(cy, inner->getBounds().fTop + innerR)) {
1229 auto avgR = (innerR + outerR) / 2.f;
1230 auto circleBounds = SkRect::MakeLTRB(cx - avgR, cy - avgR, cx + avgR, cy + avgR);
1231 SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle);
1232 stroke.setStrokeStyle(outerR - innerR);
Greg Daniel2655ede2019-04-10 00:49:28 +00001233 auto op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix,
Robert Phillips7c525e62018-06-12 10:11:12 -04001234 circleBounds, GrStyle(stroke, nullptr),
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001235 this->caps()->shaderCaps());
Brian Salomon65749212017-12-01 16:01:47 -05001236 if (op) {
1237 this->addDrawOp(clip, std::move(op));
1238 return true;
1239 }
Mike Klein16885072018-12-11 09:54:31 -05001240 assert_alive(paint);
Brian Salomon65749212017-12-01 16:01:47 -05001241 }
1242 }
1243
Ethan Nicholas0f3c7322017-11-09 14:51:17 -05001244 GrClipEdgeType innerEdgeType, outerEdgeType;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001245 if (GrAAType::kCoverage == aaType) {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001246 innerEdgeType = GrClipEdgeType::kInverseFillAA;
1247 outerEdgeType = GrClipEdgeType::kFillAA;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001248 } else {
Ethan Nicholas1706f842017-11-10 11:58:19 -05001249 innerEdgeType = GrClipEdgeType::kInverseFillBW;
1250 outerEdgeType = GrClipEdgeType::kFillBW;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001251 }
robertphillips00095892016-02-29 13:50:40 -08001252
robertphillips00095892016-02-29 13:50:40 -08001253 SkMatrix inverseVM;
1254 if (!viewMatrix.isIdentity()) {
1255 if (!origInner.transform(viewMatrix, inner.writable())) {
1256 return false;
1257 }
1258 if (!origOuter.transform(viewMatrix, outer.writable())) {
1259 return false;
1260 }
1261 if (!viewMatrix.invert(&inverseVM)) {
1262 return false;
1263 }
1264 } else {
1265 inverseVM.reset();
halcanary9d524f22016-03-29 09:03:52 -07001266 }
robertphillips00095892016-02-29 13:50:40 -08001267
Ethan Nicholaseace9352018-10-15 20:09:54 +00001268 const auto& caps = *this->caps()->shaderCaps();
robertphillips00095892016-02-29 13:50:40 -08001269 // TODO these need to be a geometry processors
Ethan Nicholaseace9352018-10-15 20:09:54 +00001270 auto innerEffect = GrRRectEffect::Make(innerEdgeType, *inner, caps);
robertphillips00095892016-02-29 13:50:40 -08001271 if (!innerEffect) {
1272 return false;
1273 }
1274
Ethan Nicholaseace9352018-10-15 20:09:54 +00001275 auto outerEffect = GrRRectEffect::Make(outerEdgeType, *outer, caps);
robertphillips00095892016-02-29 13:50:40 -08001276 if (!outerEffect) {
1277 return false;
1278 }
1279
Brian Salomon82f44312017-01-11 13:42:54 -05001280 paint.addCoverageFragmentProcessor(std::move(innerEffect));
1281 paint.addCoverageFragmentProcessor(std::move(outerEffect));
robertphillips00095892016-02-29 13:50:40 -08001282
1283 SkRect bounds = outer->getBounds();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001284 if (GrAAType::kCoverage == aaType) {
robertphillips00095892016-02-29 13:50:40 -08001285 bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
1286 }
halcanary9d524f22016-03-29 09:03:52 -07001287
Brian Salomon82f44312017-01-11 13:42:54 -05001288 this->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), bounds,
1289 inverseVM);
robertphillips00095892016-02-29 13:50:40 -08001290 return true;
1291}
1292
Brian Osman11052242016-10-27 14:47:55 -04001293void GrRenderTargetContext::drawDRRect(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001294 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001295 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001296 const SkMatrix& viewMatrix,
1297 const SkRRect& outer,
1298 const SkRRect& inner) {
robertphillips00095892016-02-29 13:50:40 -08001299 ASSERT_SINGLE_OWNER
1300 RETURN_IF_ABANDONED
1301 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001302 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawDRRect", fContext);
robertphillips00095892016-02-29 13:50:40 -08001303
1304 SkASSERT(!outer.isEmpty());
1305 SkASSERT(!inner.isEmpty());
1306
Robert Phillips72152832017-01-25 17:31:35 -05001307 AutoCheckFlush acf(this->drawingManager());
robertphillips00095892016-02-29 13:50:40 -08001308
Brian Salomon82f44312017-01-11 13:42:54 -05001309 if (this->drawFilledDRRect(clip, std::move(paint), aa, viewMatrix, outer, inner)) {
robertphillips00095892016-02-29 13:50:40 -08001310 return;
1311 }
Mike Klein16885072018-12-11 09:54:31 -05001312 assert_alive(paint);
robertphillips00095892016-02-29 13:50:40 -08001313
1314 SkPath path;
1315 path.setIsVolatile(true);
1316 path.addRRect(inner);
1317 path.addRRect(outer);
Mike Reed3a509812019-11-25 12:34:17 -05001318 path.setFillType(SkPathFillType::kEvenOdd);
Brian Salomon2fad74a2017-12-20 13:28:55 -05001319 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, GrShape(path));
robertphillips00095892016-02-29 13:50:40 -08001320}
1321
robertphillipsea461502015-05-26 11:38:03 -07001322///////////////////////////////////////////////////////////////////////////////
1323
Brian Osman11052242016-10-27 14:47:55 -04001324void GrRenderTargetContext::drawRegion(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001325 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001326 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001327 const SkMatrix& viewMatrix,
1328 const SkRegion& region,
Stan Iliev73d8fd92017-08-02 15:36:24 -04001329 const GrStyle& style,
1330 const GrUserStencilSettings* ss) {
msarettcc319b92016-08-25 18:07:18 -07001331 ASSERT_SINGLE_OWNER
1332 RETURN_IF_ABANDONED
1333 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001334 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawRegion", fContext);
msarettcc319b92016-08-25 18:07:18 -07001335
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001336 if (GrAA::kYes == aa) {
Brian Salomonfc527d22016-12-14 21:07:01 -05001337 // GrRegionOp performs no antialiasing but is much faster, so here we check the matrix
Brian Salomonc57c7c92016-12-06 14:47:34 -05001338 // to see whether aa is really required.
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001339 if (!SkToBool(viewMatrix.getType() & ~(SkMatrix::kTranslate_Mask)) &&
Brian Salomon34169692017-08-28 15:32:01 -04001340 SkScalarIsInt(viewMatrix.getTranslateX()) &&
1341 SkScalarIsInt(viewMatrix.getTranslateY())) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001342 aa = GrAA::kNo;
1343 }
Brian Salomonc57c7c92016-12-06 14:47:34 -05001344 }
msarettcc319b92016-08-25 18:07:18 -07001345 bool complexStyle = !style.isSimpleFill();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001346 if (complexStyle || GrAA::kYes == aa) {
msarettcc319b92016-08-25 18:07:18 -07001347 SkPath path;
1348 region.getBoundaryPath(&path);
Robert Phillips46a13382018-08-23 13:53:01 -04001349 path.setIsVolatile(true);
1350
Brian Salomon82f44312017-01-11 13:42:54 -05001351 return this->drawPath(clip, std::move(paint), aa, viewMatrix, path, style);
msarettcc319b92016-08-25 18:07:18 -07001352 }
1353
Chris Dalton7d6748e2019-03-13 00:34:52 -06001354 GrAAType aaType = this->chooseAAType(GrAA::kNo);
Robert Phillips7c525e62018-06-12 10:11:12 -04001355 std::unique_ptr<GrDrawOp> op = GrRegionOp::Make(fContext, std::move(paint), viewMatrix, region,
1356 aaType, ss);
Brian Salomonf0366322017-07-11 15:53:05 -04001357 this->addDrawOp(clip, std::move(op));
msarettcc319b92016-08-25 18:07:18 -07001358}
1359
Brian Osman11052242016-10-27 14:47:55 -04001360void GrRenderTargetContext::drawOval(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001361 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001362 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001363 const SkMatrix& viewMatrix,
1364 const SkRect& oval,
1365 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001366 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001367 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001368 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001369 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawOval", fContext);
robertphillips2e1e51f2015-10-15 08:01:48 -07001370
Robert Phillips7484d202018-07-03 09:09:08 -04001371 const SkStrokeRec& stroke = style.strokeRec();
1372
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001373 if (oval.isEmpty() && !style.pathEffect()) {
Robert Phillips7484d202018-07-03 09:09:08 -04001374 if (stroke.getStyle() == SkStrokeRec::kFill_Style) {
1375 return;
1376 }
1377
1378 this->drawRect(clip, std::move(paint), aa, viewMatrix, oval, &style);
Brian Salomon62e4f3d2018-04-20 13:54:11 -04001379 return;
robertphillipsea461502015-05-26 11:38:03 -07001380 }
1381
Robert Phillips72152832017-01-25 17:31:35 -05001382 AutoCheckFlush acf(this->drawingManager());
csmartdaltona7f29642016-07-07 08:49:11 -07001383
Chris Dalton7d6748e2019-03-13 00:34:52 -06001384 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton0dffbab2019-03-27 13:08:50 -06001385
1386 std::unique_ptr<GrDrawOp> op;
Jim Van Verthd7871cc2019-06-27 13:08:04 -04001387 if (GrAAType::kCoverage == aaType && oval.width() > SK_ScalarNearlyZero &&
1388 oval.width() == oval.height() && viewMatrix.isSimilarity()) {
Jim Van Verth64b85892019-06-17 12:01:46 -04001389 // We don't draw true circles as round rects in coverage mode, because it can
1390 // cause perf regressions on some platforms as compared to the dedicated circle Op.
1391 assert_alive(paint);
1392 op = GrOvalOpFactory::MakeCircleOp(fContext, std::move(paint), viewMatrix, oval, style,
1393 this->caps()->shaderCaps());
1394 }
1395 if (!op && style.isSimpleFill()) {
Chris Dalton82eb9e72019-03-21 14:26:39 -06001396 // GrFillRRectOp has special geometry and a fragment-shader branch to conditionally evaluate
1397 // the arc equation. This same special geometry and fragment branch also turn out to be a
1398 // substantial optimization for drawing ovals (namely, by not evaluating the arc equation
1399 // inside the oval's inner diamond). Given these optimizations, it's a clear win to draw
1400 // ovals the exact same way we do round rects.
Jim Van Verth64b85892019-06-17 12:01:46 -04001401 assert_alive(paint);
1402 op = GrFillRRectOp::Make(fContext, aaType, viewMatrix, SkRRect::MakeOval(oval),
1403 *this->caps(), std::move(paint));
Chris Dalton0dffbab2019-03-27 13:08:50 -06001404 }
Greg Daniel2655ede2019-04-10 00:49:28 +00001405 if (!op && GrAAType::kCoverage == aaType) {
Chris Dalton0dffbab2019-03-27 13:08:50 -06001406 assert_alive(paint);
Greg Daniel2655ede2019-04-10 00:49:28 +00001407 op = GrOvalOpFactory::MakeOvalOp(fContext, std::move(paint), viewMatrix, oval, style,
1408 this->caps()->shaderCaps());
Chris Dalton0dffbab2019-03-27 13:08:50 -06001409 }
1410 if (op) {
1411 this->addDrawOp(clip, std::move(op));
1412 return;
robertphillipsea461502015-05-26 11:38:03 -07001413 }
robertphillipsb56f9272016-02-25 11:03:52 -08001414
Mike Klein16885072018-12-11 09:54:31 -05001415 assert_alive(paint);
Brian Salomon5209d7f2018-04-20 16:52:42 -04001416 this->drawShapeUsingPathRenderer(
1417 clip, std::move(paint), aa, viewMatrix,
Mike Reed30bc5272019-11-22 18:34:02 +00001418 GrShape(SkRRect::MakeOval(oval), SkPathDirection::kCW, 2, false, style));
robertphillipsea461502015-05-26 11:38:03 -07001419}
1420
Brian Osman11052242016-10-27 14:47:55 -04001421void GrRenderTargetContext::drawArc(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001422 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001423 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001424 const SkMatrix& viewMatrix,
1425 const SkRect& oval,
1426 SkScalar startAngle,
1427 SkScalar sweepAngle,
1428 bool useCenter,
1429 const GrStyle& style) {
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001430 ASSERT_SINGLE_OWNER
1431 RETURN_IF_ABANDONED
1432 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001433 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawArc", fContext);
Robert Phillipsf1d0ced2017-02-10 09:31:01 -05001434
1435 AutoCheckFlush acf(this->drawingManager());
1436
Chris Dalton7d6748e2019-03-13 00:34:52 -06001437 GrAAType aaType = this->chooseAAType(aa);
Greg Daniel2655ede2019-04-10 00:49:28 +00001438 if (GrAAType::kCoverage == aaType) {
1439 const GrShaderCaps* shaderCaps = this->caps()->shaderCaps();
1440 std::unique_ptr<GrDrawOp> op = GrOvalOpFactory::MakeArcOp(fContext,
1441 std::move(paint),
1442 viewMatrix,
1443 oval,
1444 startAngle,
1445 sweepAngle,
1446 useCenter,
1447 style,
1448 shaderCaps);
1449 if (op) {
1450 this->addDrawOp(clip, std::move(op));
1451 return;
1452 }
1453 assert_alive(paint);
bsalomon4f3a0ca2016-08-22 13:14:26 -07001454 }
Brian Salomone4949402018-04-26 15:22:04 -04001455 this->drawShapeUsingPathRenderer(
1456 clip, std::move(paint), aa, viewMatrix,
1457 GrShape::MakeArc(oval, startAngle, sweepAngle, useCenter, style));
bsalomon4f3a0ca2016-08-22 13:14:26 -07001458}
1459
Brian Osman11052242016-10-27 14:47:55 -04001460void GrRenderTargetContext::drawImageLattice(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001461 GrPaint&& paint,
Brian Osman11052242016-10-27 14:47:55 -04001462 const SkMatrix& viewMatrix,
Brian Salomon2a943df2018-05-04 13:43:19 -04001463 sk_sp<GrTextureProxy> image,
Greg Danielc594e622019-10-15 14:01:49 -04001464 GrColorType srcColorType,
Brian Salomon2a943df2018-05-04 13:43:19 -04001465 sk_sp<GrColorSpaceXform> csxf,
1466 GrSamplerState::Filter filter,
Brian Osman11052242016-10-27 14:47:55 -04001467 std::unique_ptr<SkLatticeIter> iter,
1468 const SkRect& dst) {
joshualitt1de610a2016-01-06 08:26:09 -08001469 ASSERT_SINGLE_OWNER
joshualitt33a5fce2015-11-18 13:28:51 -08001470 RETURN_IF_ABANDONED
1471 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001472 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawImageLattice", fContext);
joshualitt33a5fce2015-11-18 13:28:51 -08001473
Robert Phillips72152832017-01-25 17:31:35 -05001474 AutoCheckFlush acf(this->drawingManager());
joshualitt33a5fce2015-11-18 13:28:51 -08001475
Brian Salomon2a943df2018-05-04 13:43:19 -04001476 std::unique_ptr<GrDrawOp> op =
Robert Phillips7c525e62018-06-12 10:11:12 -04001477 GrLatticeOp::MakeNonAA(fContext, std::move(paint), viewMatrix, std::move(image),
Greg Danielc594e622019-10-15 14:01:49 -04001478 srcColorType, std::move(csxf), filter, std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -04001479 this->addDrawOp(clip, std::move(op));
joshualitt33a5fce2015-11-18 13:28:51 -08001480}
1481
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001482void GrRenderTargetContext::drawDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable,
1483 const SkRect& bounds) {
1484 std::unique_ptr<GrOp> op(GrDrawableOp::Make(fContext, std::move(drawable), bounds));
1485 SkASSERT(op);
Chris Dalton08755122019-08-05 16:13:47 -06001486 this->addOp(std::move(op));
Greg Daniel64cc9aa2018-10-19 13:54:56 -04001487}
1488
Brian Salomon031b0ba2019-05-23 11:05:26 -04001489void GrRenderTargetContext::asyncRescaleAndReadPixels(
1490 const SkImageInfo& info, const SkIRect& srcRect, SkSurface::RescaleGamma rescaleGamma,
1491 SkFilterQuality rescaleQuality, ReadPixelsCallback callback, ReadPixelsContext context) {
1492 auto direct = fContext->priv().asDirectContext();
1493 if (!direct) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001494 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001495 return;
1496 }
1497 if (fRenderTargetProxy->wrapsVkSecondaryCB()) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001498 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001499 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001500 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001501 auto dstCT = SkColorTypeToGrColorType(info.colorType());
Brian Salomonb3bd8642019-11-04 16:59:29 -05001502 if (dstCT == GrColorType::kUnknown) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001503 callback(context, nullptr);
Brian Salomon5dd77462019-09-26 16:57:09 -04001504 return;
1505 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001506 bool needsRescale = srcRect.width() != info.width() || srcRect.height() != info.height();
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001507 auto colorTypeOfFinalContext = this->colorInfo().colorType();
Brian Salomonf30b1c12019-06-20 12:25:02 -04001508 auto backendFormatOfFinalContext = fRenderTargetProxy->backendFormat();
Brian Salomon031b0ba2019-05-23 11:05:26 -04001509 if (needsRescale) {
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001510 colorTypeOfFinalContext = dstCT;
Robert Phillips0a15cc62019-07-30 12:49:10 -04001511 backendFormatOfFinalContext = this->caps()->getDefaultBackendFormat(dstCT,
1512 GrRenderable::kYes);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001513 }
Greg Danielc6dc5cf2019-07-17 16:02:00 -04001514 auto readInfo = this->caps()->supportedReadPixelsColorType(colorTypeOfFinalContext,
Brian Salomonf30b1c12019-06-20 12:25:02 -04001515 backendFormatOfFinalContext, dstCT);
1516 // Fail if we can't read from the source surface's color type.
1517 if (readInfo.fColorType == GrColorType::kUnknown) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001518 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001519 return;
1520 }
Brian Salomon624f9062019-07-02 14:23:00 -04001521 // Fail if read color type does not have all of dstCT's color channels and those missing color
1522 // channels are in the src.
1523 uint32_t dstComponents = GrColorTypeComponentFlags(dstCT);
1524 uint32_t legalReadComponents = GrColorTypeComponentFlags(readInfo.fColorType);
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001525 uint32_t srcComponents = GrColorTypeComponentFlags(this->colorInfo().colorType());
Brian Salomon624f9062019-07-02 14:23:00 -04001526 if ((~legalReadComponents & dstComponents) & srcComponents) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001527 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001528 return;
1529 }
1530
Brian Salomonbf6b9792019-08-21 09:38:10 -04001531 std::unique_ptr<GrRenderTargetContext> tempRTC;
Brian Salomon031b0ba2019-05-23 11:05:26 -04001532 int x = srcRect.fLeft;
1533 int y = srcRect.fTop;
1534 if (needsRescale) {
Brian Salomonbf6b9792019-08-21 09:38:10 -04001535 tempRTC = this->rescale(info, srcRect, rescaleGamma, rescaleQuality);
1536 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001537 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001538 return;
1539 }
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001540 SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace()));
Brian Salomonbf6b9792019-08-21 09:38:10 -04001541 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001542 x = y = 0;
1543 } else {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001544 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(this->colorInfo().colorSpace(),
1545 this->colorInfo().alphaType(),
1546 info.colorSpace(),
1547 info.alphaType());
Brian Salomon031b0ba2019-05-23 11:05:26 -04001548 // Insert a draw to a temporary surface if we need to do a y-flip or color space conversion.
1549 if (this->origin() == kBottomLeft_GrSurfaceOrigin || xform) {
Brian Salomon4d036892019-07-02 15:10:58 -04001550 // We flip or color convert by drawing and we don't currently support drawing to
1551 // kPremul.
1552 if (info.alphaType() == kUnpremul_SkAlphaType) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001553 callback(context, nullptr);
Brian Salomon4d036892019-07-02 15:10:58 -04001554 return;
1555 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001556 sk_sp<GrTextureProxy> texProxy = sk_ref_sp(fRenderTargetProxy->asTextureProxy());
Brian Salomon031b0ba2019-05-23 11:05:26 -04001557 SkRect srcRectToDraw = SkRect::Make(srcRect);
1558 // If the src is not texturable first try to make a copy to a texture.
1559 if (!texProxy) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04001560 texProxy = GrSurfaceProxy::Copy(fContext, fRenderTargetProxy.get(),
1561 GrMipMapped::kNo, srcRect, SkBackingFit::kApprox,
1562 SkBudgeted::kNo);
1563 if (!texProxy) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001564 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001565 return;
1566 }
Brian Salomon031b0ba2019-05-23 11:05:26 -04001567 srcRectToDraw = SkRect::MakeWH(srcRect.width(), srcRect.height());
1568 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001569 tempRTC = direct->priv().makeDeferredRenderTargetContext(
Brian Salomon27ae52c2019-07-03 11:27:44 -04001570 SkBackingFit::kApprox, srcRect.width(), srcRect.height(),
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001571 this->colorInfo().colorType(), info.refColorSpace(), 1, GrMipMapped::kNo,
Brian Salomon27ae52c2019-07-03 11:27:44 -04001572 kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001573 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001574 callback(context, nullptr);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001575 return;
1576 }
Greg Danielc594e622019-10-15 14:01:49 -04001577 tempRTC->drawTexture(GrNoClip(), std::move(texProxy), this->colorInfo().colorType(),
Brian Salomonfc118442019-11-22 19:09:27 -05001578 this->colorInfo().alphaType(), GrSamplerState::Filter::kNearest,
1579 SkBlendMode::kSrc, SK_PMColor4fWHITE, srcRectToDraw,
Brian Salomonbf6b9792019-08-21 09:38:10 -04001580 SkRect::MakeWH(srcRect.width(), srcRect.height()), GrAA::kNo,
1581 GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint,
1582 SkMatrix::I(), std::move(xform));
Brian Salomon031b0ba2019-05-23 11:05:26 -04001583 x = y = 0;
Brian Salomon031b0ba2019-05-23 11:05:26 -04001584 }
1585 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001586 auto rtc = tempRTC ? tempRTC.get() : this;
Brian Salomon024bd002019-06-11 11:38:16 -04001587 return rtc->asyncReadPixels(SkIRect::MakeXYWH(x, y, info.width(), info.height()),
1588 info.colorType(), callback, context);
Brian Salomon031b0ba2019-05-23 11:05:26 -04001589}
1590
Brian Salomon9241a6d2019-10-03 13:26:54 -04001591class GrRenderTargetContext::AsyncReadResult : public SkSurface::AsyncReadResult {
1592public:
1593 AsyncReadResult(uint32_t inboxID) : fInboxID(inboxID) {}
1594 ~AsyncReadResult() override {
1595 for (int i = 0; i < fPlanes.count(); ++i) {
1596 if (!fPlanes[i].fMappedBuffer) {
1597 delete[] static_cast<const char*>(fPlanes[i].fData);
1598 } else {
1599 GrClientMappedBufferManager::BufferFinishedMessageBus::Post(
1600 {std::move(fPlanes[i].fMappedBuffer), fInboxID});
1601 }
1602 }
1603 }
1604
1605 int count() const override { return fPlanes.count(); }
1606 const void* data(int i) const override { return fPlanes[i].fData; }
1607 size_t rowBytes(int i) const override { return fPlanes[i].fRowBytes; }
1608
1609 bool addTransferResult(const PixelTransferResult& result,
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001610 SkISize dimensions,
Brian Salomon9241a6d2019-10-03 13:26:54 -04001611 size_t rowBytes,
1612 GrClientMappedBufferManager* manager) {
1613 SkASSERT(!result.fTransferBuffer->isMapped());
1614 const void* mappedData = result.fTransferBuffer->map();
1615 if (!mappedData) {
1616 return false;
1617 }
1618 if (result.fPixelConverter) {
Brian Salomon9f2b86c2019-10-22 10:37:46 -04001619 std::unique_ptr<char[]> convertedData(new char[rowBytes * dimensions.height()]);
Brian Salomon9241a6d2019-10-03 13:26:54 -04001620 result.fPixelConverter(convertedData.get(), mappedData);
1621 this->addCpuPlane(std::move(convertedData), rowBytes);
1622 result.fTransferBuffer->unmap();
1623 } else {
1624 manager->insert(result.fTransferBuffer);
1625 this->addMappedPlane(mappedData, rowBytes, std::move(result.fTransferBuffer));
1626 }
1627 return true;
1628 }
1629
1630 void addCpuPlane(std::unique_ptr<const char[]> data, size_t rowBytes) {
1631 SkASSERT(data);
1632 SkASSERT(rowBytes > 0);
1633 fPlanes.emplace_back(data.release(), rowBytes, nullptr);
1634 }
1635
1636private:
1637 void addMappedPlane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> mappedBuffer) {
1638 SkASSERT(data);
1639 SkASSERT(rowBytes > 0);
1640 SkASSERT(mappedBuffer);
1641 SkASSERT(mappedBuffer->isMapped());
1642 fPlanes.emplace_back(data, rowBytes, std::move(mappedBuffer));
1643 }
1644
1645 struct Plane {
1646 Plane(const void* data, size_t rowBytes, sk_sp<GrGpuBuffer> buffer)
1647 : fData(data), fRowBytes(rowBytes), fMappedBuffer(std::move(buffer)) {}
1648 const void* fData;
1649 size_t fRowBytes;
1650 // If this is null then fData is heap alloc and must be delete[]ed as const char[].
1651 sk_sp<GrGpuBuffer> fMappedBuffer;
1652 };
1653 SkSTArray<3, Plane> fPlanes;
1654 uint32_t fInboxID;
1655};
1656
Brian Salomon024bd002019-06-11 11:38:16 -04001657void GrRenderTargetContext::asyncReadPixels(const SkIRect& rect, SkColorType colorType,
1658 ReadPixelsCallback callback,
1659 ReadPixelsContext context) {
1660 SkASSERT(rect.fLeft >= 0 && rect.fRight <= this->width());
1661 SkASSERT(rect.fTop >= 0 && rect.fBottom <= this->height());
1662
Emircan Uysaler76974762019-10-25 13:28:54 -04001663 if (this->asSurfaceProxy()->isProtected()) {
1664 callback(context, nullptr);
1665 return;
1666 }
1667
Brian Salomon9241a6d2019-10-03 13:26:54 -04001668 auto directContext = fContext->priv().asDirectContext();
1669 SkASSERT(directContext);
1670 auto mappedBufferManager = directContext->priv().clientMappedBufferManager();
1671
Brian Salomon024bd002019-06-11 11:38:16 -04001672 auto transferResult = this->transferPixels(SkColorTypeToGrColorType(colorType), rect);
1673
1674 if (!transferResult.fTransferBuffer) {
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001675 auto ii = SkImageInfo::Make(rect.size(), colorType,
1676 this->colorInfo().alphaType(),
1677 this->colorInfo().refColorSpace());
Brian Salomon9241a6d2019-10-03 13:26:54 -04001678 auto result = skstd::make_unique<AsyncReadResult>(0);
1679 std::unique_ptr<char[]> data(new char[ii.computeMinByteSize()]);
1680 SkPixmap pm(ii, data.get(), ii.minRowBytes());
1681 result->addCpuPlane(std::move(data), pm.rowBytes());
1682
Brian Salomon1d435302019-07-01 13:05:28 -04001683 if (!this->readPixels(ii, pm.writable_addr(), pm.rowBytes(), {rect.fLeft, rect.fTop})) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001684 callback(context, nullptr);
Brian Salomonab32f652019-05-10 14:24:50 -04001685 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04001686 callback(context, std::move(result));
Brian Salomon031b0ba2019-05-23 11:05:26 -04001687 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001688 }
Brian Salomoncd734f62019-05-10 16:32:54 -04001689
Brian Salomonab32f652019-05-10 14:24:50 -04001690 struct FinishContext {
1691 ReadPixelsCallback* fClientCallback;
1692 ReadPixelsContext fClientContext;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001693 SkISize fSize;
Brian Salomon024bd002019-06-11 11:38:16 -04001694 SkColorType fColorType;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001695 GrClientMappedBufferManager* fMappedBufferManager;
Brian Salomon024bd002019-06-11 11:38:16 -04001696 PixelTransferResult fTransferResult;
Brian Salomonab32f652019-05-10 14:24:50 -04001697 };
1698 // Assumption is that the caller would like to flush. We could take a parameter or require an
1699 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
1700 // callback to GrGpu until after the next flush that flushes our op list, though.
Brian Salomon9241a6d2019-10-03 13:26:54 -04001701 auto* finishContext = new FinishContext{callback,
1702 context,
1703 rect.size(),
1704 colorType,
1705 mappedBufferManager,
1706 std::move(transferResult)};
Brian Salomonab32f652019-05-10 14:24:50 -04001707 auto finishCallback = [](GrGpuFinishedContext c) {
Brian Salomon024bd002019-06-11 11:38:16 -04001708 const auto* context = reinterpret_cast<const FinishContext*>(c);
Brian Salomon9241a6d2019-10-03 13:26:54 -04001709 auto result = skstd::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
1710 size_t rowBytes = context->fSize.width() * SkColorTypeBytesPerPixel(context->fColorType);
1711 if (!result->addTransferResult(context->fTransferResult, context->fSize, rowBytes,
1712 context->fMappedBufferManager)) {
1713 result.reset();
Brian Salomoncd734f62019-05-10 16:32:54 -04001714 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04001715 (*context->fClientCallback)(context->fClientContext, std::move(result));
Brian Salomon024bd002019-06-11 11:38:16 -04001716 delete context;
1717 };
1718 GrFlushInfo flushInfo;
1719 flushInfo.fFinishedContext = finishContext;
1720 flushInfo.fFinishedProc = finishCallback;
1721 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo);
1722}
1723
Brian Salomon9241a6d2019-10-03 13:26:54 -04001724void GrRenderTargetContext::asyncRescaleAndReadPixelsYUV420(SkYUVColorSpace yuvColorSpace,
1725 sk_sp<SkColorSpace> dstColorSpace,
1726 const SkIRect& srcRect,
1727 const SkISize& dstSize,
1728 RescaleGamma rescaleGamma,
1729 SkFilterQuality rescaleQuality,
1730 ReadPixelsCallback callback,
1731 ReadPixelsContext context) {
Brian Salomon024bd002019-06-11 11:38:16 -04001732 SkASSERT(srcRect.fLeft >= 0 && srcRect.fRight <= this->width());
1733 SkASSERT(srcRect.fTop >= 0 && srcRect.fBottom <= this->height());
Brian Salomon9241a6d2019-10-03 13:26:54 -04001734 SkASSERT(!dstSize.isZero());
1735 SkASSERT((dstSize.width() % 2 == 0) && (dstSize.height() % 2 == 0));
1736
Brian Salomon024bd002019-06-11 11:38:16 -04001737 auto direct = fContext->priv().asDirectContext();
1738 if (!direct) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001739 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001740 return;
1741 }
1742 if (fRenderTargetProxy->wrapsVkSecondaryCB()) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001743 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001744 return;
1745 }
Emircan Uysaler76974762019-10-25 13:28:54 -04001746 if (this->asSurfaceProxy()->isProtected()) {
1747 callback(context, nullptr);
1748 return;
1749 }
Brian Salomon024bd002019-06-11 11:38:16 -04001750 int x = srcRect.fLeft;
1751 int y = srcRect.fTop;
Brian Salomonbf6b9792019-08-21 09:38:10 -04001752 std::unique_ptr<GrRenderTargetContext> tempRTC;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001753 bool needsRescale = srcRect.size() != dstSize;
Brian Salomon024bd002019-06-11 11:38:16 -04001754 if (needsRescale) {
Brian Salomon4d036892019-07-02 15:10:58 -04001755 // We assume the caller wants kPremul. There is no way to indicate a preference.
Brian Salomon9241a6d2019-10-03 13:26:54 -04001756 auto info = SkImageInfo::Make(dstSize, kRGBA_8888_SkColorType, kPremul_SkAlphaType,
Brian Salomon024bd002019-06-11 11:38:16 -04001757 dstColorSpace);
1758 // TODO: Incorporate the YUV conversion into last pass of rescaling.
Brian Salomonbf6b9792019-08-21 09:38:10 -04001759 tempRTC = this->rescale(info, srcRect, rescaleGamma, rescaleQuality);
1760 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001761 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001762 return;
Brian Salomonab32f652019-05-10 14:24:50 -04001763 }
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001764 SkASSERT(SkColorSpace::Equals(tempRTC->colorInfo().colorSpace(), info.colorSpace()));
Brian Salomonbf6b9792019-08-21 09:38:10 -04001765 SkASSERT(tempRTC->origin() == kTopLeft_GrSurfaceOrigin);
Brian Salomon024bd002019-06-11 11:38:16 -04001766 x = y = 0;
1767 } else {
Brian Salomon4d036892019-07-02 15:10:58 -04001768 // We assume the caller wants kPremul. There is no way to indicate a preference.
1769 sk_sp<GrColorSpaceXform> xform = GrColorSpaceXform::Make(
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04001770 this->colorInfo().colorSpace(), this->colorInfo().alphaType(), dstColorSpace.get(),
1771 kPremul_SkAlphaType);
Brian Salomon024bd002019-06-11 11:38:16 -04001772 if (xform) {
1773 sk_sp<GrTextureProxy> texProxy = this->asTextureProxyRef();
1774 // TODO: Do something if the input is not a texture already.
1775 if (!texProxy) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001776 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001777 return;
1778 }
Brian Salomon024bd002019-06-11 11:38:16 -04001779 SkRect srcRectToDraw = SkRect::Make(srcRect);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001780 tempRTC = direct->priv().makeDeferredRenderTargetContext(
Brian Salomon9241a6d2019-10-03 13:26:54 -04001781 SkBackingFit::kApprox, dstSize.width(), dstSize.height(),
1782 this->colorInfo().colorType(), dstColorSpace, 1, GrMipMapped::kNo,
1783 kTopLeft_GrSurfaceOrigin);
Brian Salomonbf6b9792019-08-21 09:38:10 -04001784 if (!tempRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001785 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001786 return;
1787 }
Greg Danielc594e622019-10-15 14:01:49 -04001788 tempRTC->drawTexture(GrNoClip(), std::move(texProxy), this->colorInfo().colorType(),
Brian Salomonfc118442019-11-22 19:09:27 -05001789 this->colorInfo().alphaType(), GrSamplerState::Filter::kNearest,
1790 SkBlendMode::kSrc, SK_PMColor4fWHITE, srcRectToDraw,
Brian Salomonbf6b9792019-08-21 09:38:10 -04001791 SkRect::MakeWH(srcRect.width(), srcRect.height()), GrAA::kNo,
1792 GrQuadAAFlags::kNone, SkCanvas::kFast_SrcRectConstraint,
1793 SkMatrix::I(), std::move(xform));
Brian Salomon024bd002019-06-11 11:38:16 -04001794 x = y = 0;
1795 }
1796 }
Brian Salomonbf6b9792019-08-21 09:38:10 -04001797 auto srcProxy = tempRTC ? tempRTC->asTextureProxyRef() : this->asTextureProxyRef();
Brian Salomon024bd002019-06-11 11:38:16 -04001798 // TODO: Do something if the input is not a texture already.
1799 if (!srcProxy) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001800 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001801 return;
1802 }
Greg Danielc594e622019-10-15 14:01:49 -04001803
Brian Salomon27ae52c2019-07-03 11:27:44 -04001804 auto yRTC = direct->priv().makeDeferredRenderTargetContextWithFallback(
Brian Salomon9241a6d2019-10-03 13:26:54 -04001805 SkBackingFit::kApprox, dstSize.width(), dstSize.height(), GrColorType::kAlpha_8,
1806 dstColorSpace, 1, GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin);
1807 int halfW = dstSize.width()/2;
1808 int halfH = dstSize.height()/2;
Brian Salomon27ae52c2019-07-03 11:27:44 -04001809 auto uRTC = direct->priv().makeDeferredRenderTargetContextWithFallback(
Brian Salomon9241a6d2019-10-03 13:26:54 -04001810 SkBackingFit::kApprox, halfW, halfH, GrColorType::kAlpha_8, dstColorSpace, 1,
Brian Salomon27ae52c2019-07-03 11:27:44 -04001811 GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin);
1812 auto vRTC = direct->priv().makeDeferredRenderTargetContextWithFallback(
Brian Salomon9241a6d2019-10-03 13:26:54 -04001813 SkBackingFit::kApprox, halfW, halfH, GrColorType::kAlpha_8, dstColorSpace, 1,
Brian Salomon27ae52c2019-07-03 11:27:44 -04001814 GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin);
Brian Salomon024bd002019-06-11 11:38:16 -04001815 if (!yRTC || !uRTC || !vRTC) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001816 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001817 return;
1818 }
1819
Brian Osman28ba5282019-10-30 14:18:07 -04001820 float baseM[20];
1821 SkColorMatrix_RGB2YUV(yuvColorSpace, baseM);
1822
Brian Salomon024bd002019-06-11 11:38:16 -04001823 // TODO: Use one transfer buffer for all three planes to reduce map/unmap cost?
1824
1825 auto texMatrix = SkMatrix::MakeTrans(x, y);
1826
Brian Salomon9241a6d2019-10-03 13:26:54 -04001827 SkRect dstRectY = SkRect::Make(dstSize);
1828 SkRect dstRectUV = SkRect::MakeWH(halfW, halfH);
Michael Ludwig61328202019-06-19 14:48:58 +00001829
Brian Salomon024bd002019-06-11 11:38:16 -04001830 // This matrix generates (r,g,b,a) = (0, 0, 0, y)
1831 float yM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04001832 std::fill_n(yM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04001833 std::copy_n(baseM + 0, 5, yM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04001834 GrPaint yPaint;
Brian Salomonfc118442019-11-22 19:09:27 -05001835 yPaint.addColorTextureProcessor(srcProxy, this->colorInfo().alphaType(), texMatrix);
Brian Salomon024bd002019-06-11 11:38:16 -04001836 auto yFP = GrColorMatrixFragmentProcessor::Make(yM, false, true, false);
1837 yPaint.addColorFragmentProcessor(std::move(yFP));
1838 yPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig61328202019-06-19 14:48:58 +00001839 yRTC->fillRectToRect(GrNoClip(), std::move(yPaint), GrAA::kNo, SkMatrix::I(),
1840 dstRectY, dstRectY);
Brian Salomon024bd002019-06-11 11:38:16 -04001841 auto yTransfer = yRTC->transferPixels(GrColorType::kAlpha_8,
1842 SkIRect::MakeWH(yRTC->width(), yRTC->height()));
1843 if (!yTransfer.fTransferBuffer) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001844 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001845 return;
1846 }
1847
1848 texMatrix.preScale(2.f, 2.f);
1849 // This matrix generates (r,g,b,a) = (0, 0, 0, u)
1850 float uM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04001851 std::fill_n(uM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04001852 std::copy_n(baseM + 5, 5, uM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04001853 GrPaint uPaint;
Brian Salomonfc118442019-11-22 19:09:27 -05001854 uPaint.addColorTextureProcessor(srcProxy, this->colorInfo().alphaType(), texMatrix,
Greg Danielc594e622019-10-15 14:01:49 -04001855 GrSamplerState::ClampBilerp());
Brian Salomon024bd002019-06-11 11:38:16 -04001856 auto uFP = GrColorMatrixFragmentProcessor::Make(uM, false, true, false);
1857 uPaint.addColorFragmentProcessor(std::move(uFP));
1858 uPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig61328202019-06-19 14:48:58 +00001859 uRTC->fillRectToRect(GrNoClip(), std::move(uPaint), GrAA::kNo, SkMatrix::I(),
1860 dstRectUV, dstRectUV);
Brian Salomon024bd002019-06-11 11:38:16 -04001861 auto uTransfer = uRTC->transferPixels(GrColorType::kAlpha_8,
1862 SkIRect::MakeWH(uRTC->width(), uRTC->height()));
1863 if (!uTransfer.fTransferBuffer) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001864 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001865 return;
1866 }
1867
1868 // This matrix generates (r,g,b,a) = (0, 0, 0, v)
1869 float vM[20];
Brian Salomondf586b72019-06-11 13:26:37 -04001870 std::fill_n(vM, 15, 0.f);
Brian Osman28ba5282019-10-30 14:18:07 -04001871 std::copy_n(baseM + 10, 5, vM + 15);
Brian Salomon024bd002019-06-11 11:38:16 -04001872 GrPaint vPaint;
Brian Salomonfc118442019-11-22 19:09:27 -05001873 vPaint.addColorTextureProcessor(srcProxy, this->colorInfo().alphaType(), texMatrix,
Greg Danielc594e622019-10-15 14:01:49 -04001874 GrSamplerState::ClampBilerp());
Brian Salomon024bd002019-06-11 11:38:16 -04001875 auto vFP = GrColorMatrixFragmentProcessor::Make(vM, false, true, false);
1876 vPaint.addColorFragmentProcessor(std::move(vFP));
1877 vPaint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Michael Ludwig61328202019-06-19 14:48:58 +00001878 vRTC->fillRectToRect(GrNoClip(), std::move(vPaint), GrAA::kNo, SkMatrix::I(),
1879 dstRectUV, dstRectUV);
Brian Salomon024bd002019-06-11 11:38:16 -04001880 auto vTransfer = vRTC->transferPixels(GrColorType::kAlpha_8,
1881 SkIRect::MakeWH(vRTC->width(), vRTC->height()));
1882 if (!vTransfer.fTransferBuffer) {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001883 callback(context, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001884 return;
1885 }
1886
1887 struct FinishContext {
Brian Salomon9241a6d2019-10-03 13:26:54 -04001888 ReadPixelsCallback* fClientCallback;
Brian Salomon024bd002019-06-11 11:38:16 -04001889 ReadPixelsContext fClientContext;
Brian Salomon9241a6d2019-10-03 13:26:54 -04001890 GrClientMappedBufferManager* fMappedBufferManager;
1891 SkISize fSize;
Brian Salomon024bd002019-06-11 11:38:16 -04001892 PixelTransferResult fYTransfer;
1893 PixelTransferResult fUTransfer;
1894 PixelTransferResult fVTransfer;
1895 };
1896 // Assumption is that the caller would like to flush. We could take a parameter or require an
1897 // explicit flush from the caller. We'd have to have a way to defer attaching the finish
1898 // callback to GrGpu until after the next flush that flushes our op list, though.
1899 auto* finishContext = new FinishContext{callback,
1900 context,
Brian Salomon9241a6d2019-10-03 13:26:54 -04001901 direct->priv().clientMappedBufferManager(),
1902 dstSize,
Brian Salomon024bd002019-06-11 11:38:16 -04001903 std::move(yTransfer),
1904 std::move(uTransfer),
1905 std::move(vTransfer)};
1906 auto finishCallback = [](GrGpuFinishedContext c) {
1907 const auto* context = reinterpret_cast<const FinishContext*>(c);
Brian Salomon9241a6d2019-10-03 13:26:54 -04001908 auto result = skstd::make_unique<AsyncReadResult>(context->fMappedBufferManager->inboxID());
1909 auto manager = context->fMappedBufferManager;
1910 size_t rowBytes = SkToSizeT(context->fSize.width());
1911 if (!result->addTransferResult(context->fYTransfer, context->fSize, rowBytes, manager)) {
1912 (*context->fClientCallback)(context->fClientContext, nullptr);
Brian Salomon024bd002019-06-11 11:38:16 -04001913 delete context;
1914 return;
1915 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04001916 rowBytes /= 2;
1917 SkISize uvSize = {context->fSize.width()/2, context->fSize.height()/2};
1918 if (!result->addTransferResult(context->fUTransfer, uvSize, rowBytes, manager)) {
1919 (*context->fClientCallback)(context->fClientContext, nullptr);
1920 delete context;
1921 return;
Brian Salomon024bd002019-06-11 11:38:16 -04001922 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04001923 if (!result->addTransferResult(context->fVTransfer, uvSize, rowBytes, manager)) {
1924 (*context->fClientCallback)(context->fClientContext, nullptr);
1925 delete context;
1926 return;
Brian Salomon024bd002019-06-11 11:38:16 -04001927 }
Brian Salomon9241a6d2019-10-03 13:26:54 -04001928 (*context->fClientCallback)(context->fClientContext, std::move(result));
Brian Salomonab32f652019-05-10 14:24:50 -04001929 delete context;
1930 };
1931 GrFlushInfo flushInfo;
1932 flushInfo.fFinishedContext = finishContext;
1933 flushInfo.fFinishedProc = finishCallback;
1934 this->flush(SkSurface::BackendSurfaceAccess::kNoAccess, flushInfo);
Brian Salomonab32f652019-05-10 14:24:50 -04001935}
1936
Greg Daniele6bfb7d2019-04-17 15:26:11 -04001937GrSemaphoresSubmitted GrRenderTargetContext::flush(SkSurface::BackendSurfaceAccess access,
1938 const GrFlushInfo& info) {
robertphillips8c523e02016-07-26 07:41:00 -07001939 ASSERT_SINGLE_OWNER
Robert Phillips6a6de562019-02-15 15:19:15 -05001940 if (fContext->priv().abandoned()) {
Robert Phillipsa9162df2019-02-11 14:12:03 -05001941 return GrSemaphoresSubmitted::kNo;
1942 }
robertphillips8c523e02016-07-26 07:41:00 -07001943 SkDEBUGCODE(this->validate();)
Robert Phillips15c91422019-05-07 16:54:48 -04001944 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "flush", fContext);
robertphillips8c523e02016-07-26 07:41:00 -07001945
Greg Daniele6bfb7d2019-04-17 15:26:11 -04001946 return this->drawingManager()->flushSurface(fRenderTargetProxy.get(), access, info);
Greg Daniela5cb7812017-06-16 09:45:32 -04001947}
1948
Greg Danielc64ee462017-06-15 16:59:49 -04001949bool GrRenderTargetContext::waitOnSemaphores(int numSemaphores,
Robert Phillipsbc4994a2019-02-14 08:36:56 -05001950 const GrBackendSemaphore waitSemaphores[]) {
Greg Daniela5cb7812017-06-16 09:45:32 -04001951 ASSERT_SINGLE_OWNER
Greg Danielc64ee462017-06-15 16:59:49 -04001952 RETURN_FALSE_IF_ABANDONED
Greg Daniela5cb7812017-06-16 09:45:32 -04001953 SkDEBUGCODE(this->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04001954 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "waitOnSemaphores", fContext);
Greg Daniela5cb7812017-06-16 09:45:32 -04001955
1956 AutoCheckFlush acf(this->drawingManager());
1957
Brian Salomon9ff5acb2019-05-08 09:04:47 -04001958 if (numSemaphores && !this->caps()->semaphoreSupport()) {
Greg Danielc64ee462017-06-15 16:59:49 -04001959 return false;
1960 }
1961
Robert Phillipsbc4994a2019-02-14 08:36:56 -05001962 auto direct = fContext->priv().asDirectContext();
1963 if (!direct) {
1964 return false;
1965 }
1966
1967 auto resourceProvider = direct->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -05001968
Greg Daniel301015c2019-11-18 14:06:46 -05001969 std::unique_ptr<std::unique_ptr<GrSemaphore>[]> grSemaphores(
1970 new std::unique_ptr<GrSemaphore>[numSemaphores]);
Greg Daniela5cb7812017-06-16 09:45:32 -04001971 for (int i = 0; i < numSemaphores; ++i) {
Greg Danielc30f1a92019-09-06 15:28:58 -04001972 grSemaphores[i] = resourceProvider->wrapBackendSemaphore(
Greg Daniel17b7c052018-01-09 13:55:33 -05001973 waitSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillWait,
1974 kAdopt_GrWrapOwnership);
Greg Daniela5cb7812017-06-16 09:45:32 -04001975 }
Greg Danielc30f1a92019-09-06 15:28:58 -04001976 this->drawingManager()->newWaitRenderTask(this->asSurfaceProxyRef(), std::move(grSemaphores),
1977 numSemaphores);
Greg Danielc64ee462017-06-15 16:59:49 -04001978 return true;
robertphillips8c523e02016-07-26 07:41:00 -07001979}
joshualitt33a5fce2015-11-18 13:28:51 -08001980
Robert Phillips65a88fa2017-08-08 08:36:22 -04001981void GrRenderTargetContext::insertEventMarker(const SkString& str) {
Robert Phillips88a32ef2018-06-07 11:05:56 -04001982 std::unique_ptr<GrOp> op(GrDebugMarkerOp::Make(fContext, fRenderTargetProxy.get(), str));
Chris Dalton08755122019-08-05 16:13:47 -06001983 this->addOp(std::move(op));
Robert Phillips65a88fa2017-08-08 08:36:22 -04001984}
1985
Brian Osman11052242016-10-27 14:47:55 -04001986void GrRenderTargetContext::drawPath(const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -05001987 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05001988 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04001989 const SkMatrix& viewMatrix,
Brian Salomon40b77a62017-12-22 11:25:52 -05001990 const SkPath& path,
Brian Osman11052242016-10-27 14:47:55 -04001991 const GrStyle& style) {
joshualitt1de610a2016-01-06 08:26:09 -08001992 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07001993 RETURN_IF_ABANDONED
robertphillips2e1e51f2015-10-15 08:01:48 -07001994 SkDEBUGCODE(this->validate();)
Robert Phillips20390c32018-08-17 11:01:03 -04001995 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawPath", fContext);
1996
Brian Salomon40b77a62017-12-22 11:25:52 -05001997 GrShape shape(path, style);
Robert Phillips20390c32018-08-17 11:01:03 -04001998
Robert Phillips27927a52018-08-20 13:18:12 -04001999 this->drawShape(clip, std::move(paint), aa, viewMatrix, shape);
Robert Phillips20390c32018-08-17 11:01:03 -04002000}
2001
2002void GrRenderTargetContext::drawShape(const GrClip& clip,
2003 GrPaint&& paint,
2004 GrAA aa,
2005 const SkMatrix& viewMatrix,
2006 const GrShape& shape) {
2007 ASSERT_SINGLE_OWNER
2008 RETURN_IF_ABANDONED
2009 SkDEBUGCODE(this->validate();)
2010 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "drawShape", fContext);
2011
Brian Salomon2fad74a2017-12-20 13:28:55 -05002012 if (shape.isEmpty()) {
2013 if (shape.inverseFilled()) {
2014 this->drawPaint(clip, std::move(paint), viewMatrix);
2015 }
2016 return;
robertphillipsea461502015-05-26 11:38:03 -07002017 }
2018
Robert Phillips72152832017-01-25 17:31:35 -05002019 AutoCheckFlush acf(this->drawingManager());
robertphillipsea461502015-05-26 11:38:03 -07002020
Brian Salomon2fad74a2017-12-20 13:28:55 -05002021 if (!shape.style().hasPathEffect()) {
Chris Dalton7d6748e2019-03-13 00:34:52 -06002022 GrAAType aaType = this->chooseAAType(aa);
Brian Salomon2fad74a2017-12-20 13:28:55 -05002023 SkRRect rrect;
2024 // We can ignore the starting point and direction since there is no path effect.
2025 bool inverted;
2026 if (shape.asRRect(&rrect, nullptr, nullptr, &inverted) && !inverted) {
2027 if (rrect.isRect()) {
2028 this->drawRect(clip, std::move(paint), aa, viewMatrix, rrect.rect(),
2029 &shape.style());
2030 return;
2031 } else if (rrect.isOval()) {
2032 this->drawOval(clip, std::move(paint), aa, viewMatrix, rrect.rect(), shape.style());
robertphillipsea461502015-05-26 11:38:03 -07002033 return;
2034 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002035 this->drawRRect(clip, std::move(paint), aa, viewMatrix, rrect, shape.style());
2036 return;
Robert Phillips73653b42018-08-22 12:42:42 -04002037 } else if (GrAAType::kCoverage == aaType && shape.style().isSimpleFill() &&
2038 viewMatrix.rectStaysRect()) {
2039 // TODO: the rectStaysRect restriction could be lifted if we were willing to apply
2040 // the matrix to all the points individually rather than just to the rect
2041 SkRect rects[2];
2042 if (shape.asNestedRects(rects)) {
2043 // Concave AA paths are expensive - try to avoid them for special cases
Michael Ludwig72ab3462018-12-10 12:43:36 -05002044 std::unique_ptr<GrDrawOp> op = GrStrokeRectOp::MakeNested(
Robert Phillips73653b42018-08-22 12:42:42 -04002045 fContext, std::move(paint), viewMatrix, rects);
2046 if (op) {
2047 this->addDrawOp(clip, std::move(op));
2048 }
2049 // Returning here indicates that there is nothing to draw in this case.
2050 return;
2051 }
robertphillipsea461502015-05-26 11:38:03 -07002052 }
2053 }
robertphillips4bc31812016-03-01 12:22:49 -08002054
Brian Salomon2fad74a2017-12-20 13:28:55 -05002055 this->drawShapeUsingPathRenderer(clip, std::move(paint), aa, viewMatrix, shape);
robertphillipsea461502015-05-26 11:38:03 -07002056}
2057
Chris Daltonbbfd5162017-11-07 13:35:22 -07002058bool GrRenderTargetContextPriv::drawAndStencilPath(const GrHardClip& clip,
Brian Osman11052242016-10-27 14:47:55 -04002059 const GrUserStencilSettings* ss,
2060 SkRegion::Op op,
2061 bool invert,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002062 GrAA aa,
Brian Osman11052242016-10-27 14:47:55 -04002063 const SkMatrix& viewMatrix,
2064 const SkPath& path) {
robertphillips391395d2016-03-02 09:26:36 -08002065 ASSERT_SINGLE_OWNER_PRIV
2066 RETURN_FALSE_IF_ABANDONED_PRIV
Brian Osman11052242016-10-27 14:47:55 -04002067 SkDEBUGCODE(fRenderTargetContext->validate();)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002068 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContextPriv", "drawAndStencilPath",
2069 fRenderTargetContext->fContext);
robertphillips391395d2016-03-02 09:26:36 -08002070
2071 if (path.isEmpty() && path.isInverseFillType()) {
Michael Ludwigaa1b6b32019-05-29 14:43:13 -04002072 GrPaint paint;
2073 paint.setCoverageSetOpXPFactory(op, invert);
2074 this->stencilRect(clip, ss, std::move(paint), GrAA::kNo, SkMatrix::I(),
2075 SkRect::MakeIWH(fRenderTargetContext->width(),
2076 fRenderTargetContext->height()));
robertphillips391395d2016-03-02 09:26:36 -08002077 return true;
2078 }
2079
Robert Phillips72152832017-01-25 17:31:35 -05002080 AutoCheckFlush acf(fRenderTargetContext->drawingManager());
robertphillips391395d2016-03-02 09:26:36 -08002081
2082 // An Assumption here is that path renderer would use some form of tweaking
2083 // the src color (either the input alpha or in the frag shader) to implement
2084 // aa. If we have some future driver-mojo path AA that can do the right
2085 // thing WRT to the blend then we'll need some query on the PR.
Chris Dalton6ce447a2019-06-23 18:07:38 -06002086 GrAAType aaType = fRenderTargetContext->chooseAAType(aa);
robertphillips976f5f02016-06-03 10:59:20 -07002087 bool hasUserStencilSettings = !ss->isUnused();
robertphillips391395d2016-03-02 09:26:36 -08002088
Chris Daltondb91c6e2017-09-08 16:25:08 -06002089 SkIRect clipConservativeBounds;
2090 clip.getConservativeBounds(fRenderTargetContext->width(), fRenderTargetContext->height(),
2091 &clipConservativeBounds, nullptr);
2092
bsalomon8acedde2016-06-24 10:42:16 -07002093 GrShape shape(path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -08002094 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04002095 canDrawArgs.fCaps = fRenderTargetContext->caps();
Chris Daltoneffee202019-07-01 22:28:03 -06002096 canDrawArgs.fProxy = fRenderTargetContext->proxy();
robertphillips391395d2016-03-02 09:26:36 -08002097 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -07002098 canDrawArgs.fShape = &shape;
Chris Daltondb91c6e2017-09-08 16:25:08 -06002099 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Chris Dalton6ce447a2019-06-23 18:07:38 -06002100 canDrawArgs.fAAType = aaType;
Greg Danielbe7fc462019-01-03 16:40:42 -05002101 SkASSERT(!fRenderTargetContext->wrapsVkSecondaryCB());
2102 canDrawArgs.fTargetIsWrappedVkSecondaryCB = false;
cdalton93a379b2016-05-11 13:58:08 -07002103 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips391395d2016-03-02 09:26:36 -08002104
2105 // Don't allow the SW renderer
Robert Phillips72152832017-01-25 17:31:35 -05002106 GrPathRenderer* pr = fRenderTargetContext->drawingManager()->getPathRenderer(
Brian Salomon36aa1762016-12-10 13:24:02 -05002107 canDrawArgs, false, GrPathRendererChain::DrawType::kStencilAndColor);
robertphillips391395d2016-03-02 09:26:36 -08002108 if (!pr) {
2109 return false;
2110 }
2111
2112 GrPaint paint;
2113 paint.setCoverageSetOpXPFactory(op, invert);
2114
Brian Salomonf3569f02017-10-24 12:52:33 -04002115 GrPathRenderer::DrawPathArgs args{fRenderTargetContext->drawingManager()->getContext(),
2116 std::move(paint),
2117 ss,
2118 fRenderTargetContext,
2119 &clip,
2120 &clipConservativeBounds,
2121 &viewMatrix,
2122 &shape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06002123 aaType,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002124 fRenderTargetContext->colorInfo().isLinearlyBlended()};
robertphillips391395d2016-03-02 09:26:36 -08002125 pr->drawPath(args);
2126 return true;
2127}
2128
Brian Osman11052242016-10-27 14:47:55 -04002129SkBudgeted GrRenderTargetContextPriv::isBudgeted() const {
robertphillips714712b2016-08-04 06:20:45 -07002130 ASSERT_SINGLE_OWNER_PRIV
2131
Robert Phillips6a6de562019-02-15 15:19:15 -05002132 if (fRenderTargetContext->fContext->priv().abandoned()) {
robertphillips714712b2016-08-04 06:20:45 -07002133 return SkBudgeted::kNo;
2134 }
2135
Brian Osman11052242016-10-27 14:47:55 -04002136 SkDEBUGCODE(fRenderTargetContext->validate();)
robertphillips714712b2016-08-04 06:20:45 -07002137
Robert Phillipsc7635fa2016-10-28 13:25:24 -04002138 return fRenderTargetContext->fRenderTargetProxy->isBudgeted();
robertphillips714712b2016-08-04 06:20:45 -07002139}
2140
Brian Salomon2fad74a2017-12-20 13:28:55 -05002141void GrRenderTargetContext::drawShapeUsingPathRenderer(const GrClip& clip,
2142 GrPaint&& paint,
2143 GrAA aa,
2144 const SkMatrix& viewMatrix,
2145 const GrShape& originalShape) {
joshualitt1de610a2016-01-06 08:26:09 -08002146 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -07002147 RETURN_IF_ABANDONED
Brian Salomondcbb9d92017-07-19 10:53:20 -04002148 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "internalDrawPath", fContext);
2149
Jim Van Verthf86073a2018-10-02 13:05:38 -04002150 if (!viewMatrix.isFinite() || !originalShape.bounds().isFinite()) {
2151 return;
2152 }
2153
Chris Daltondb91c6e2017-09-08 16:25:08 -06002154 SkIRect clipConservativeBounds;
2155 clip.getConservativeBounds(this->width(), this->height(), &clipConservativeBounds, nullptr);
2156
Brian Salomon2fad74a2017-12-20 13:28:55 -05002157 GrShape tempShape;
Chris Dalton6ce447a2019-06-23 18:07:38 -06002158 GrAAType aaType = this->chooseAAType(aa);
Chris Dalton09e56892019-03-13 00:22:01 -06002159
robertphillips68737822015-10-29 12:12:21 -07002160 GrPathRenderer::CanDrawPathArgs canDrawArgs;
Brian Salomonc7fe0f72018-05-11 10:14:21 -04002161 canDrawArgs.fCaps = this->caps();
Chris Daltoneffee202019-07-01 22:28:03 -06002162 canDrawArgs.fProxy = this->proxy();
robertphillips68737822015-10-29 12:12:21 -07002163 canDrawArgs.fViewMatrix = &viewMatrix;
Brian Salomon2fad74a2017-12-20 13:28:55 -05002164 canDrawArgs.fShape = &originalShape;
Chris Daltondb91c6e2017-09-08 16:25:08 -06002165 canDrawArgs.fClipConservativeBounds = &clipConservativeBounds;
Greg Danielbe7fc462019-01-03 16:40:42 -05002166 canDrawArgs.fTargetIsWrappedVkSecondaryCB = this->wrapsVkSecondaryCB();
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002167 canDrawArgs.fHasUserStencilSettings = false;
robertphillips68737822015-10-29 12:12:21 -07002168
Brian Salomon0e8fc8b2016-12-09 15:10:07 -05002169 GrPathRenderer* pr;
Brian Salomon82125e92016-12-10 09:35:48 -05002170 static constexpr GrPathRendererChain::DrawType kType = GrPathRendererChain::DrawType::kColor;
Brian Salomon2fad74a2017-12-20 13:28:55 -05002171 if (originalShape.isEmpty() && !originalShape.inverseFilled()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002172 return;
2173 }
2174
Chris Dalton6ce447a2019-06-23 18:07:38 -06002175 canDrawArgs.fAAType = aaType;
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002176
2177 // Try a 1st time without applying any of the style to the geometry (and barring sw)
2178 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
2179 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
2180
Brian Salomon2fad74a2017-12-20 13:28:55 -05002181 if (!pr && originalShape.style().pathEffect()) {
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002182 // It didn't work above, so try again with the path effect applied.
Brian Salomon2fad74a2017-12-20 13:28:55 -05002183 tempShape = originalShape.applyStyle(GrStyle::Apply::kPathEffectOnly, styleScale);
2184 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07002185 return;
2186 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002187 canDrawArgs.fShape = &tempShape;
Robert Phillips72152832017-01-25 17:31:35 -05002188 pr = this->drawingManager()->getPathRenderer(canDrawArgs, false, kType);
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002189 }
2190 if (!pr) {
Brian Salomon2fad74a2017-12-20 13:28:55 -05002191 if (canDrawArgs.fShape->style().applies()) {
2192 tempShape = canDrawArgs.fShape->applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec,
2193 styleScale);
2194 if (tempShape.isEmpty()) {
robertphillipsea461502015-05-26 11:38:03 -07002195 return;
2196 }
Brian Salomon2fad74a2017-12-20 13:28:55 -05002197 canDrawArgs.fShape = &tempShape;
Brian Salomone7df0bb2018-05-07 14:44:57 -04002198 // This time, allow SW renderer
2199 pr = this->drawingManager()->getPathRenderer(canDrawArgs, true, kType);
2200 } else {
2201 pr = this->drawingManager()->getSoftwarePathRenderer();
bsalomon6663acf2016-05-10 09:14:17 -07002202 }
Brian Salomon1e5d0ca2017-12-14 10:50:19 -05002203 }
robertphillipsea461502015-05-26 11:38:03 -07002204
bsalomon8acedde2016-06-24 10:42:16 -07002205 if (!pr) {
robertphillipsea461502015-05-26 11:38:03 -07002206#ifdef SK_DEBUG
2207 SkDebugf("Unable to find path renderer compatible with path.\n");
2208#endif
2209 return;
2210 }
2211
Robert Phillips256c37b2017-03-01 14:32:46 -05002212 GrPathRenderer::DrawPathArgs args{this->drawingManager()->getContext(),
Brian Salomon82f44312017-01-11 13:42:54 -05002213 std::move(paint),
2214 &GrUserStencilSettings::kUnused,
2215 this,
2216 &clip,
Chris Daltondb91c6e2017-09-08 16:25:08 -06002217 &clipConservativeBounds,
Brian Salomon82f44312017-01-11 13:42:54 -05002218 &viewMatrix,
Brian Salomon2fad74a2017-12-20 13:28:55 -05002219 canDrawArgs.fShape,
Chris Dalton6ce447a2019-06-23 18:07:38 -06002220 aaType,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002221 this->colorInfo().isLinearlyBlended()};
bsalomon0aff2fa2015-07-31 06:48:27 -07002222 pr->drawPath(args);
robertphillipsea461502015-05-26 11:38:03 -07002223}
2224
Brian Salomon467921e2017-03-06 16:17:12 -05002225static void op_bounds(SkRect* bounds, const GrOp* op) {
2226 *bounds = op->bounds();
2227 if (op->hasZeroArea()) {
2228 if (op->hasAABloat()) {
2229 bounds->outset(0.5f, 0.5f);
2230 } else {
2231 // We don't know which way the particular GPU will snap lines or points at integer
2232 // coords. So we ensure that the bounds is large enough for either snap.
2233 SkRect before = *bounds;
2234 bounds->roundOut(bounds);
2235 if (bounds->fLeft == before.fLeft) {
2236 bounds->fLeft -= 1;
2237 }
2238 if (bounds->fTop == before.fTop) {
2239 bounds->fTop -= 1;
2240 }
2241 if (bounds->fRight == before.fRight) {
2242 bounds->fRight += 1;
2243 }
2244 if (bounds->fBottom == before.fBottom) {
2245 bounds->fBottom += 1;
2246 }
2247 }
2248 }
2249}
2250
Chris Dalton08755122019-08-05 16:13:47 -06002251void GrRenderTargetContext::addOp(std::unique_ptr<GrOp> op) {
Greg Danielf41b2bd2019-08-22 16:19:24 -04002252 this->getOpsTask()->addOp(
Chris Dalton08755122019-08-05 16:13:47 -06002253 std::move(op), GrTextureResolveManager(this->drawingManager()), *this->caps());
2254}
2255
Brian Salomon348a0372018-10-31 10:42:18 -04002256void GrRenderTargetContext::addDrawOp(const GrClip& clip, std::unique_ptr<GrDrawOp> op,
2257 const std::function<WillAddOpFn>& willAddFn) {
joshualitt1de610a2016-01-06 08:26:09 -08002258 ASSERT_SINGLE_OWNER
Robert Phillips69893702019-02-22 11:16:30 -05002259 if (fContext->priv().abandoned()) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002260 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002261 return;
Robert Phillipsc0138922017-03-08 11:50:55 -05002262 }
robertphillips2e1e51f2015-10-15 08:01:48 -07002263 SkDEBUGCODE(this->validate();)
Ethan Nicholas029b22c2018-10-18 16:49:56 -04002264 SkDEBUGCODE(op->fAddDrawOpCalled = true;)
Brian Salomondcbb9d92017-07-19 10:53:20 -04002265 GR_CREATE_TRACE_MARKER_CONTEXT("GrRenderTargetContext", "addDrawOp", fContext);
robertphillips2d70dcb2015-10-06 07:38:23 -07002266
Brian Salomon467921e2017-03-06 16:17:12 -05002267 // Setup clip
2268 SkRect bounds;
2269 op_bounds(&bounds, op.get());
Brian Salomon97180af2017-03-14 13:42:58 -04002270 GrAppliedClip appliedClip;
Brian Salomon54d212e2017-03-21 14:22:38 -04002271 GrDrawOp::FixedFunctionFlags fixedFunctionFlags = op->fixedFunctionFlags();
Chris Dalton6b982802019-06-27 13:53:46 -06002272 bool usesHWAA = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesHWAA;
Chris Dalton9acf6822019-11-12 11:52:40 -07002273 bool usesUserStencilBits = fixedFunctionFlags & GrDrawOp::FixedFunctionFlags::kUsesStencil;
Chris Dalton6b982802019-06-27 13:53:46 -06002274
Chris Dalton9acf6822019-11-12 11:52:40 -07002275 if (usesUserStencilBits) { // Stencil clipping will call setNeedsStencil on its own, if needed.
Chris Daltoneffee202019-07-01 22:28:03 -06002276 this->setNeedsStencil(usesHWAA);
Chris Dalton6b982802019-06-27 13:53:46 -06002277 }
2278
Chris Dalton9acf6822019-11-12 11:52:40 -07002279 if (!clip.apply(fContext, this, usesHWAA, usesUserStencilBits, &appliedClip, &bounds)) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002280 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002281 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04002282 }
2283
Chris Dalton9acf6822019-11-12 11:52:40 -07002284 bool willUseStencil = usesUserStencilBits || appliedClip.hasStencilClip();
2285 SkASSERT(!willUseStencil || fNumStencilSamples > 0);
2286
2287 // If stencil is enabled and the framebuffer is mixed sampled, then the graphics pipeline will
2288 // have mixed sampled coverage, regardless of whether HWAA is enabled. (e.g., a non-aa draw
2289 // that uses a stencil test when the stencil buffer is multisampled.)
2290 bool hasMixedSampledCoverage = (
2291 willUseStencil && fNumStencilSamples > this->numSamples());
2292 SkASSERT(!hasMixedSampledCoverage || fRenderTargetProxy->canUseMixedSamples(*this->caps()));
Brian Salomon54d212e2017-03-21 14:22:38 -04002293
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002294 GrClampType clampType = GrColorTypeClampType(this->colorInfo().colorType());
Brian Osman5ced0bf2019-03-15 10:15:29 -04002295 GrProcessorSet::Analysis analysis = op->finalize(
Chris Dalton6ce447a2019-06-23 18:07:38 -06002296 *this->caps(), &appliedClip, hasMixedSampledCoverage, clampType);
2297
Greg Daniel524e28b2019-11-01 11:48:53 -04002298 GrXferProcessor::DstProxyView dstProxyView;
Chris Dalton945ee652019-01-23 09:10:36 -07002299 if (analysis.requiresDstTexture()) {
Greg Daniel524e28b2019-11-01 11:48:53 -04002300 if (!this->setupDstProxyView(clip, *op, &dstProxyView)) {
Robert Phillips9da87e02019-02-04 13:26:26 -05002301 fContext->priv().opMemoryPool()->release(std::move(op));
Brian Salomon348a0372018-10-31 10:42:18 -04002302 return;
Brian Salomon54d212e2017-03-21 14:22:38 -04002303 }
2304 }
2305
2306 op->setClippedBounds(bounds);
Greg Danielf41b2bd2019-08-22 16:19:24 -04002307 auto opsTask = this->getOpsTask();
Brian Salomon348a0372018-10-31 10:42:18 -04002308 if (willAddFn) {
Greg Danielf41b2bd2019-08-22 16:19:24 -04002309 willAddFn(op.get(), opsTask->uniqueID());
Brian Salomon348a0372018-10-31 10:42:18 -04002310 }
Greg Daniel524e28b2019-11-01 11:48:53 -04002311 opsTask->addDrawOp(std::move(op), analysis, std::move(appliedClip), dstProxyView,
Greg Danielf41b2bd2019-08-22 16:19:24 -04002312 GrTextureResolveManager(this->drawingManager()), *this->caps());
Brian Salomon54d212e2017-03-21 14:22:38 -04002313}
2314
Greg Daniel524e28b2019-11-01 11:48:53 -04002315bool GrRenderTargetContext::setupDstProxyView(const GrClip& clip, const GrOp& op,
2316 GrXferProcessor::DstProxyView* dstProxyView) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002317 // If we are wrapping a vulkan secondary command buffer, we can't make a dst copy because we
2318 // don't actually have a VkImage to make a copy of. Additionally we don't have the power to
2319 // start and stop the render pass in order to make the copy.
Chris Dalton2243c7b2019-08-21 14:46:35 -06002320 if (fRenderTargetProxy->wrapsVkSecondaryCB()) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002321 return false;
2322 }
2323
Chris Dalton2243c7b2019-08-21 14:46:35 -06002324 if (this->caps()->textureBarrierSupport() && !fRenderTargetProxy->requiresManualMSAAResolve()) {
Greg Daniel524e28b2019-11-01 11:48:53 -04002325 if (fRenderTargetProxy->asTextureProxy()) {
Brian Salomon467921e2017-03-06 16:17:12 -05002326 // The render target is a texture, so we can read from it directly in the shader. The XP
2327 // will be responsible to detect this situation and request a texture barrier.
Greg Daniel524e28b2019-11-01 11:48:53 -04002328 dstProxyView->setProxyView(this->textureSurfaceView());
2329 dstProxyView->setOffset(0, 0);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002330 return true;
Brian Salomon467921e2017-03-06 16:17:12 -05002331 }
2332 }
2333
Brian Salomon9f2b86c2019-10-22 10:37:46 -04002334 SkIRect copyRect = SkIRect::MakeSize(fRenderTargetProxy->dimensions());
Brian Salomon467921e2017-03-06 16:17:12 -05002335
Eric Karl74480882017-04-03 14:49:05 -07002336 SkIRect clippedRect;
Chris Dalton2243c7b2019-08-21 14:46:35 -06002337 clip.getConservativeBounds(
2338 fRenderTargetProxy->width(), fRenderTargetProxy->height(), &clippedRect);
Brian Salomon09181ef2018-11-14 13:39:51 -05002339 SkRect opBounds = op.bounds();
2340 // If the op has aa bloating or is a infinitely thin geometry (hairline) outset the bounds by
2341 // 0.5 pixels.
2342 if (op.hasAABloat() || op.hasZeroArea()) {
2343 opBounds.outset(0.5f, 0.5f);
2344 // An antialiased/hairline draw can sometimes bleed outside of the clips bounds. For
2345 // performance we may ignore the clip when the draw is entirely inside the clip is float
2346 // space but will hit pixels just outside the clip when actually rasterizing.
2347 clippedRect.outset(1, 1);
Brian Salomon9f2b86c2019-10-22 10:37:46 -04002348 clippedRect.intersect(SkIRect::MakeSize(fRenderTargetProxy->dimensions()));
Brian Salomon09181ef2018-11-14 13:39:51 -05002349 }
2350 SkIRect opIBounds;
2351 opBounds.roundOut(&opIBounds);
2352 if (!clippedRect.intersect(opIBounds)) {
Brian Salomon467921e2017-03-06 16:17:12 -05002353#ifdef SK_DEBUG
Robert Phillipsbf25d432017-04-07 10:08:53 -04002354 GrCapsDebugf(this->caps(), "setupDstTexture: Missed an early reject bailing on draw.");
Brian Salomon467921e2017-03-06 16:17:12 -05002355#endif
Robert Phillipsbf25d432017-04-07 10:08:53 -04002356 return false;
Brian Salomon467921e2017-03-06 16:17:12 -05002357 }
2358
2359 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
2360 // have per-sample dst values by making the copy multisampled.
Greg Daniel0258c902019-08-01 13:08:33 -04002361 GrCaps::DstCopyRestrictions restrictions = this->caps()->getDstCopyRestrictions(
Brian Salomon4bc0c1f2019-09-30 15:12:27 -04002362 fRenderTargetProxy.get(), this->colorInfo().colorType());
Brian Salomon467921e2017-03-06 16:17:12 -05002363
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002364 if (!restrictions.fMustCopyWholeSrc) {
Eric Karl74480882017-04-03 14:49:05 -07002365 copyRect = clippedRect;
2366 }
Brian Salomon467921e2017-03-06 16:17:12 -05002367
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002368 SkIPoint dstOffset;
Robert Phillipsbf25d432017-04-07 10:08:53 -04002369 SkBackingFit fit;
Brian Salomon1e03b6b12019-07-17 17:28:24 -04002370 if (restrictions.fRectsMustMatch == GrSurfaceProxy::RectsMustMatch::kYes) {
Eric Karl74480882017-04-03 14:49:05 -07002371 dstOffset = {0, 0};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002372 fit = SkBackingFit::kExact;
Eric Karl74480882017-04-03 14:49:05 -07002373 } else {
Eric Karl74480882017-04-03 14:49:05 -07002374 dstOffset = {copyRect.fLeft, copyRect.fTop};
Robert Phillipsbf25d432017-04-07 10:08:53 -04002375 fit = SkBackingFit::kApprox;
Eric Karl74480882017-04-03 14:49:05 -07002376 }
Brian Salomonfc118442019-11-22 19:09:27 -05002377 sk_sp<GrTextureProxy> newProxy =
2378 GrSurfaceProxy::Copy(fContext, fRenderTargetProxy.get(), GrMipMapped::kNo, copyRect,
2379 fit, SkBudgeted::kYes, restrictions.fRectsMustMatch);
Greg Daniel46cfbc62019-06-07 11:43:30 -04002380 SkASSERT(newProxy);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002381
Greg Daniel524e28b2019-11-01 11:48:53 -04002382 dstProxyView->setProxyView({std::move(newProxy), this->origin(), this->textureSwizzle()});
2383 dstProxyView->setOffset(dstOffset);
Robert Phillipsbf25d432017-04-07 10:08:53 -04002384 return true;
robertphillips2334fb62015-06-17 05:43:33 -07002385}
Greg Daniel46cfbc62019-06-07 11:43:30 -04002386
Brian Salomonfc118442019-11-22 19:09:27 -05002387bool GrRenderTargetContext::blitTexture(GrTextureProxy* src, const SkIRect& srcRect,
2388 const SkIPoint& dstPoint) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04002389 SkIRect clippedSrcRect;
2390 SkIPoint clippedDstPoint;
Brian Salomon9f2b86c2019-10-22 10:37:46 -04002391 if (!GrClipSrcRectAndDstPoint(this->asSurfaceProxy()->dimensions(), src->dimensions(), srcRect,
2392 dstPoint, &clippedSrcRect, &clippedDstPoint)) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04002393 return false;
2394 }
2395
2396 GrPaint paint;
2397 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomonfc118442019-11-22 19:09:27 -05002398 auto fp = GrSimpleTextureEffect::Make(sk_ref_sp(src), kUnknown_SkAlphaType, SkMatrix::I());
Greg Daniel46cfbc62019-06-07 11:43:30 -04002399 if (!fp) {
2400 return false;
2401 }
2402 paint.addColorFragmentProcessor(std::move(fp));
2403
2404 this->fillRectToRect(
2405 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
2406 SkRect::MakeXYWH(clippedDstPoint.fX, clippedDstPoint.fY, clippedSrcRect.width(),
2407 clippedSrcRect.height()),
2408 SkRect::Make(clippedSrcRect));
2409 return true;
2410}