• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2021 Google LLC
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "src/gpu/ganesh/SurfaceFillContext.h"
9 
10 #include "include/core/SkBlendMode.h"
11 #include "include/gpu/ganesh/GrRecordingContext.h"
12 #include "include/private/base/SingleOwner.h"
13 #include "include/private/base/SkAssert.h"
14 #include "include/private/base/SkPoint_impl.h"
15 #include "include/private/gpu/ganesh/GrTypesPriv.h"
16 #include "src/core/SkTraceEvent.h"
17 #include "src/gpu/Swizzle.h"
18 #include "src/gpu/ganesh/GrAppliedClip.h"
19 #include "src/gpu/ganesh/GrAuditTrail.h"
20 #include "src/gpu/ganesh/GrCaps.h"
21 #include "src/gpu/ganesh/GrDrawingManager.h"
22 #include "src/gpu/ganesh/GrDstProxyView.h"
23 #include "src/gpu/ganesh/GrPaint.h"
24 #include "src/gpu/ganesh/GrProcessorSet.h"
25 #include "src/gpu/ganesh/GrRecordingContextPriv.h"
26 #include "src/gpu/ganesh/GrRenderTask.h"
27 #include "src/gpu/ganesh/GrScissorState.h"
28 #include "src/gpu/ganesh/GrTextureResolveManager.h"
29 #include "src/gpu/ganesh/GrTracing.h"
30 #include "src/gpu/ganesh/effects/GrMatrixEffect.h"
31 #include "src/gpu/ganesh/effects/GrTextureEffect.h"
32 #include "src/gpu/ganesh/geometry/GrRect.h"
33 #include "src/gpu/ganesh/ops/ClearOp.h"
34 #include "src/gpu/ganesh/ops/FillRectOp.h"
35 #include "src/gpu/ganesh/ops/GrDrawOp.h"
36 
37 #include <cstdint>
38 
39 #define ASSERT_SINGLE_OWNER        SKGPU_ASSERT_SINGLE_OWNER(this->singleOwner())
40 #define RETURN_IF_ABANDONED        if (fContext->abandoned()) { return; }
41 
42 namespace skgpu::ganesh {
43 
44 // In MDB mode the reffing of the 'getLastOpsTask' call's result allows in-progress
45 // OpsTask to be picked up and added to by SurfaceFillContext lower in the call
46 // stack. When this occurs with a closed OpsTask, a new one will be allocated
47 // when the SurfaceFillContext attempts to use it (via getOpsTask).
SurfaceFillContext(GrRecordingContext * rContext,GrSurfaceProxyView readView,GrSurfaceProxyView writeView,const GrColorInfo & colorInfo)48 SurfaceFillContext::SurfaceFillContext(GrRecordingContext* rContext,
49                                        GrSurfaceProxyView readView,
50                                        GrSurfaceProxyView writeView,
51                                        const GrColorInfo& colorInfo)
52             : SurfaceContext(rContext, std::move(readView), colorInfo)
53             , fWriteView(std::move(writeView)) {
54     SkASSERT(this->asSurfaceProxy() == fWriteView.proxy());
55     SkASSERT(this->origin() == fWriteView.origin());
56 
57     fOpsTask = sk_ref_sp(rContext->priv().drawingManager()->getLastOpsTask(this->asSurfaceProxy()));
58 
59     SkDEBUGCODE(this->validate();)
60 }
61 
getOpsTask()62 OpsTask* SurfaceFillContext::getOpsTask() {
63     ASSERT_SINGLE_OWNER
64     SkDEBUGCODE(this->validate();)
65 
66     if (!fOpsTask || fOpsTask->isClosed()) {
67         this->replaceOpsTask();
68     }
69     SkASSERT(!fOpsTask->isClosed());
70     return fOpsTask.get();
71 }
72 
discard()73 void SurfaceFillContext::discard() {
74     ASSERT_SINGLE_OWNER
75     RETURN_IF_ABANDONED
76     SkDEBUGCODE(this->validate();)
77     GR_CREATE_TRACE_MARKER_CONTEXT("SurfaceFillContext", "discard", fContext);
78 
79     this->getOpsTask()->discard();
80 }
81 
resolveMSAA()82 void SurfaceFillContext::resolveMSAA() {
83     ASSERT_SINGLE_OWNER
84     RETURN_IF_ABANDONED
85     SkDEBUGCODE(this->validate();)
86     GR_CREATE_TRACE_MARKER_CONTEXT("SurfaceFillContext", "resolveMSAA", fContext);
87 
88     this->drawingManager()->newTextureResolveRenderTask(this->asSurfaceProxyRef(),
89                                                         GrSurfaceProxy::ResolveFlags::kMSAA,
90                                                         *this->caps());
91 }
92 
fillRectWithFP(const SkIRect & dstRect,std::unique_ptr<GrFragmentProcessor> fp)93 void SurfaceFillContext::fillRectWithFP(const SkIRect& dstRect,
94                                         std::unique_ptr<GrFragmentProcessor> fp) {
95     ASSERT_SINGLE_OWNER
96     RETURN_IF_ABANDONED
97     SkDEBUGCODE(this->validate();)
98     GR_CREATE_TRACE_MARKER_CONTEXT("SurfaceFillContext", "fillRectWithFP", fContext);
99 
100     GrPaint paint;
101     paint.setColorFragmentProcessor(std::move(fp));
102     paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
103     auto op = FillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
104                                         SkRect::Make(dstRect));
105     this->addDrawOp(std::move(op));
106 }
107 
fillRectWithFP(const SkIRect & dstRect,const SkMatrix & localMatrix,std::unique_ptr<GrFragmentProcessor> fp)108 void SurfaceFillContext::fillRectWithFP(const SkIRect& dstRect,
109                                         const SkMatrix& localMatrix,
110                                         std::unique_ptr<GrFragmentProcessor> fp) {
111     fp = GrMatrixEffect::Make(localMatrix, std::move(fp));
112     this->fillRectWithFP(dstRect, std::move(fp));
113 }
114 
blitTexture(GrSurfaceProxyView view,const SkIRect & srcRect,const SkIPoint & dstPoint)115 bool SurfaceFillContext::blitTexture(GrSurfaceProxyView view,
116                                      const SkIRect& srcRect,
117                                      const SkIPoint& dstPoint) {
118     SkASSERT(view.asTextureProxy());
119 
120     SkIPoint clippedDstPoint = dstPoint;
121     SkIRect clippedSrcRect = srcRect;
122     if (!GrClipSrcRectAndDstPoint(this->dimensions(),
123                                   &clippedDstPoint,
124                                   view.dimensions(),
125                                   &clippedSrcRect)) {
126         return false;
127     }
128 
129     SkIRect clippedDstRect = SkIRect::MakePtSize(clippedDstPoint, clippedSrcRect.size());
130 
131     auto fp = GrTextureEffect::Make(std::move(view), kUnknown_SkAlphaType);
132     this->fillRectToRectWithFP(SkRect::Make(clippedSrcRect), clippedDstRect, std::move(fp));
133     return true;
134 }
135 
refRenderTask()136 sk_sp<GrRenderTask> SurfaceFillContext::refRenderTask() {
137     return sk_ref_sp(this->getOpsTask());
138 }
139 
replaceOpsTask()140 OpsTask* SurfaceFillContext::replaceOpsTask() {
141     sk_sp<OpsTask> newOpsTask = this->drawingManager()->newOpsTask(this->writeSurfaceView(),
142                                                                    this->arenas());
143     this->willReplaceOpsTask(fOpsTask.get(), newOpsTask.get());
144     fOpsTask = std::move(newOpsTask);
145     return fOpsTask.get();
146 }
147 
ClearToGrPaint(std::array<float,4> color,GrPaint * paint)148 void SurfaceFillContext::ClearToGrPaint(std::array<float, 4> color, GrPaint* paint) {
149     paint->setColor4f({color[0], color[1], color[2], color[3]});
150     if (color[3] == 1.f) {
151         // Can just rely on the src-over blend mode to do the right thing.
152         // This may improve batching.
153         paint->setPorterDuffXPFactory(SkBlendMode::kSrcOver);
154     } else {
155         // A clear overwrites the prior color, so even if it's transparent, it behaves as if it
156         // were src blended
157         paint->setPorterDuffXPFactory(SkBlendMode::kSrc);
158     }
159 }
160 
addOp(GrOp::Owner op)161 void SurfaceFillContext::addOp(GrOp::Owner op) {
162     GrDrawingManager* drawingMgr = this->drawingManager();
163     this->getOpsTask()->addOp(drawingMgr,
164                               std::move(op),
165                               GrTextureResolveManager(drawingMgr),
166                               *this->caps());
167 }
168 
169 
addDrawOp(GrOp::Owner owner)170 void SurfaceFillContext::addDrawOp(GrOp::Owner owner) {
171     GrDrawOp* op = static_cast<GrDrawOp*>(owner.get());
172     GrClampType clampType = GrColorTypeClampType(this->colorInfo().colorType());
173     auto clip = GrAppliedClip::Disabled();
174     const GrCaps& caps = *this->caps();
175     GrProcessorSet::Analysis analysis = op->finalize(caps, &clip, clampType);
176     SkASSERT(!op->usesStencil());
177     SkASSERT(!analysis.requiresDstTexture());
178     SkRect bounds = owner->bounds();
179     // We shouldn't have coverage AA or hairline draws in fill contexts.
180     SkASSERT(!op->hasAABloat() && !op->hasZeroArea());
181     if (!bounds.intersect(this->asSurfaceProxy()->getBoundsRect())) {
182         return;
183     }
184     op->setClippedBounds(op->bounds());
185     SkDEBUGCODE(op->fAddDrawOpCalled = true;)
186 
187     GrDstProxyView dstProxyView;
188     this->getOpsTask()->addDrawOp(fContext->priv().drawingManager(),
189                                   std::move(owner),
190                                   op->usesMSAA(),
191                                   analysis,
192                                   std::move(clip),
193                                   dstProxyView,
194                                   GrTextureResolveManager(this->drawingManager()),
195                                   caps);
196 }
197 
internalClear(const SkIRect * scissor,std::array<float,4> color,bool upgradePartialToFull)198 void SurfaceFillContext::internalClear(const SkIRect* scissor,
199                                        std::array<float, 4> color,
200                                        bool upgradePartialToFull) {
201     ASSERT_SINGLE_OWNER
202     RETURN_IF_ABANDONED
203     SkDEBUGCODE(this->validate();)
204     GR_CREATE_TRACE_MARKER_CONTEXT("SurfaceFillContext", "clear", fContext);
205 
206     // There are three ways clears are handled: load ops, native clears, and draws. Load ops are
207     // only for fullscreen clears; native clears can be fullscreen or with scissors if the backend
208     // supports then. Drawing an axis-aligned rect is the fallback path.
209     GrScissorState scissorState(this->asSurfaceProxy()->backingStoreDimensions());
210     if (scissor && !scissorState.set(*scissor)) {
211         // The clear is offscreen, so skip it (normally this would be handled by addDrawOp,
212         // except clear ops are not draw ops).
213         return;
214     }
215 
216     // If we have a scissor but it's okay to clear beyond it for performance reasons, then disable
217     // the test. We only do this when the clear would be handled by a load op or natively.
218     if (scissorState.enabled() && !this->caps()->performColorClearsAsDraws()) {
219         if (upgradePartialToFull && (this->caps()->preferFullscreenClears() ||
220                                      this->caps()->shouldInitializeTextures())) {
221             // TODO: wrt the shouldInitializeTextures path, it would be more performant to
222             // only clear the entire target if we knew it had not been cleared before. As
223             // is this could end up doing a lot of redundant clears.
224             scissorState.setDisabled();
225         } else {
226             // Unlike with stencil clears, we also allow clears up to the logical dimensions of the
227             // render target to overflow into any approx-fit padding of the backing store dimensions
228             scissorState.relaxTest(this->dimensions());
229         }
230     }
231 
232     if (!scissorState.enabled()) {
233         // This is a fullscreen clear, so could be handled as a load op. Regardless, we can also
234         // discard all prior ops in the current task since the color buffer will be overwritten.
235         auto opsTask = this->getOpsTask();
236         if (opsTask->resetForFullscreenClear(this->canDiscardPreviousOpsOnFullClear()) &&
237             !this->caps()->performColorClearsAsDraws()) {
238             color = this->writeSurfaceView().swizzle().applyTo(color);
239             // The op list was emptied and native clears are allowed, so just use the load op
240             opsTask->setColorLoadOp(GrLoadOp::kClear, color);
241             return;
242         } else {
243             // Will use an op for the clear, reset the load op to discard since the op will
244             // blow away the color buffer contents
245             opsTask->setColorLoadOp(GrLoadOp::kDiscard);
246         }
247     }
248 
249     // At this point we are either a partial clear or a fullscreen clear that couldn't be applied
250     // as a load op.
251     bool clearAsDraw = this->caps()->performColorClearsAsDraws() ||
252                        (scissorState.enabled() && this->caps()->performPartialClearsAsDraws());
253     if (clearAsDraw) {
254         GrPaint paint;
255         ClearToGrPaint(color, &paint);
256         auto op = FillRectOp::MakeNonAARect(fContext, std::move(paint), SkMatrix::I(),
257                                             SkRect::Make(scissorState.rect()));
258         this->addDrawOp(std::move(op));
259     } else {
260         color = this->writeSurfaceView().swizzle().applyTo(color);
261         this->addOp(ClearOp::MakeColor(fContext, scissorState, color));
262     }
263 }
264 
265 #ifdef SK_DEBUG
onValidate() const266 void SurfaceFillContext::onValidate() const {
267     if (fOpsTask && !fOpsTask->isClosed()) {
268         SkASSERT(this->drawingManager()->getLastRenderTask(fWriteView.proxy()) == fOpsTask.get());
269     }
270 }
271 #endif
272 
273 }  // namespace skgpu::ganesh
274