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